blob: 16c589e6a89904a1092f12f430bfb7e4dbccaffa [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,
325 CryptoType()
326 .set_property_name("Speck128/128-XTS")
327 .set_crypto_name("speck128-xts-plain64")
328 .set_keysize(32),
329 // Add new CryptoTypes here. Order is not important.
330};
331
332
333// ---------- START COMPILE-TIME SANITY CHECK BLOCK -------------------------
334// We confirm all supported_crypto_types have a small enough keysize and
335// had both set_property_name() and set_crypto_name() called.
336
337template <typename T, size_t N>
338constexpr size_t array_length(T (&)[N]) { return N; }
339
340constexpr bool indexOutOfBoundsForCryptoTypes(size_t index) {
341 return (index >= array_length(supported_crypto_types));
342}
343
344constexpr bool isValidCryptoType(const CryptoType &crypto_type) {
345 return ((crypto_type.get_property_name() != nullptr) &&
346 (crypto_type.get_crypto_name() != nullptr) &&
347 (crypto_type.get_keysize() <= MAX_KEY_LEN));
348}
349
350// Note in C++11 that constexpr functions can only have a single line.
351// So our code is a bit convoluted (using recursion instead of a loop),
352// but it's asserting at compile time that all of our key lengths are valid.
353constexpr bool validateSupportedCryptoTypes(size_t index) {
354 return indexOutOfBoundsForCryptoTypes(index) ||
355 (isValidCryptoType(supported_crypto_types[index]) &&
356 validateSupportedCryptoTypes(index + 1));
357}
358
359static_assert(validateSupportedCryptoTypes(0),
360 "We have a CryptoType with keysize > MAX_KEY_LEN or which was "
361 "incompletely constructed.");
362// ---------- END COMPILE-TIME SANITY CHECK BLOCK -------------------------
363
364
365// Don't call this directly, use get_crypto_type(), which caches this result.
366const CryptoType &CryptoType::get_device_crypto_algorithm() {
367 constexpr char CRYPT_ALGO_PROP[] = "ro.crypto.fde_algorithm";
368 char paramstr[PROPERTY_VALUE_MAX];
369
370 property_get(CRYPT_ALGO_PROP, paramstr,
371 default_crypto_type.get_property_name());
372 for (auto const &ctype : supported_crypto_types) {
373 if (strcmp(paramstr, ctype.get_property_name()) == 0) {
374 return ctype;
375 }
376 }
377 ALOGE("Invalid name (%s) for %s. Defaulting to %s\n", paramstr,
378 CRYPT_ALGO_PROP, default_crypto_type.get_property_name());
379 return default_crypto_type;
380}
381
382} // namespace
383
384
385
Kenny Rootc4c70f12013-06-14 12:11:38 -0700386/**
387 * Gets the default device scrypt parameters for key derivation time tuning.
388 * The parameters should lead to about one second derivation time for the
389 * given device.
390 */
391static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700392 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000393 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700394
Paul Crowley63c18d32016-02-10 14:02:47 +0000395 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
396 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
397 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
398 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700399 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000400 ftr->N_factor = Nf;
401 ftr->r_factor = rf;
402 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700403}
404
Greg Kaiser57f9af62018-02-16 13:13:58 -0800405uint32_t cryptfs_get_keysize() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800406 return get_crypto_type().get_keysize();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800407}
408
409const char *cryptfs_get_crypto_name() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800410 return get_crypto_type().get_crypto_name();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800411}
412
Ken Sumrall3ed82362011-01-28 23:31:16 -0800413static unsigned int get_fs_size(char *dev)
414{
415 int fd, block_size;
416 struct ext4_super_block sb;
417 off64_t len;
418
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700419 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800420 SLOGE("Cannot open device to get filesystem size ");
421 return 0;
422 }
423
424 if (lseek64(fd, 1024, SEEK_SET) < 0) {
425 SLOGE("Cannot seek to superblock");
426 return 0;
427 }
428
429 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
430 SLOGE("Cannot read superblock");
431 return 0;
432 }
433
434 close(fd);
435
Daniel Rosenberge82df162014-08-15 22:19:23 +0000436 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
437 SLOGE("Not a valid ext4 superblock");
438 return 0;
439 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800440 block_size = 1024 << sb.s_log_block_size;
441 /* compute length in bytes */
442 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
443
444 /* return length in sectors */
445 return (unsigned int) (len / 512);
446}
447
Ken Sumrall160b4d62013-04-22 12:15:39 -0700448static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
449{
450 static int cached_data = 0;
451 static off64_t cached_off = 0;
452 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
453 int fd;
454 char key_loc[PROPERTY_VALUE_MAX];
455 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700456 int rc = -1;
457
458 if (!cached_data) {
Paul Crowleye2ee1522017-09-26 14:05:26 -0700459 fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700460
461 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700462 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700463 SLOGE("Cannot open real block device %s\n", real_blkdev);
464 return -1;
465 }
466
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900467 unsigned long nr_sec = 0;
468 get_blkdev_size(fd, &nr_sec);
469 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700470 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
471 * encryption info footer and key, and plenty of bytes to spare for future
472 * growth.
473 */
474 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
475 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
476 cached_data = 1;
477 } else {
478 SLOGE("Cannot get size of block device %s\n", real_blkdev);
479 }
480 close(fd);
481 } else {
482 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
483 cached_off = 0;
484 cached_data = 1;
485 }
486 }
487
488 if (cached_data) {
489 if (metadata_fname) {
490 *metadata_fname = cached_metadata_fname;
491 }
492 if (off) {
493 *off = cached_off;
494 }
495 rc = 0;
496 }
497
498 return rc;
499}
500
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800501/* Set sha256 checksum in structure */
502static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
503{
504 SHA256_CTX c;
505 SHA256_Init(&c);
506 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
507 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
508 SHA256_Final(crypt_ftr->sha256, &c);
509}
510
Ken Sumralle8744072011-01-18 22:01:55 -0800511/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800512 * update the failed mount count but not change the key.
513 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700514static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800515{
516 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800517 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700518 /* starting_off is set to the SEEK_SET offset
519 * where the crypto structure starts
520 */
521 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800522 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700523 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700524 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800525
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800526 set_ftr_sha(crypt_ftr);
527
Ken Sumrall160b4d62013-04-22 12:15:39 -0700528 if (get_crypt_ftr_info(&fname, &starting_off)) {
529 SLOGE("Unable to get crypt_ftr_info\n");
530 return -1;
531 }
532 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700533 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700534 return -1;
535 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700536 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700537 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700538 return -1;
539 }
540
541 /* Seek to the start of the crypt footer */
542 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
543 SLOGE("Cannot seek to real block device footer\n");
544 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800545 }
546
547 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
548 SLOGE("Cannot write real block device footer\n");
549 goto errout;
550 }
551
Ken Sumrall3be890f2011-09-14 16:53:46 -0700552 fstat(fd, &statbuf);
553 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700554 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700555 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800556 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800557 goto errout;
558 }
559 }
560
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800561 /* Success! */
562 rc = 0;
563
564errout:
565 close(fd);
566 return rc;
567
568}
569
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800570static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
571{
572 struct crypt_mnt_ftr copy;
573 memcpy(&copy, crypt_ftr, sizeof(copy));
574 set_ftr_sha(&copy);
575 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
576}
577
Ken Sumrall160b4d62013-04-22 12:15:39 -0700578static inline int unix_read(int fd, void* buff, int len)
579{
580 return TEMP_FAILURE_RETRY(read(fd, buff, len));
581}
582
583static inline int unix_write(int fd, const void* buff, int len)
584{
585 return TEMP_FAILURE_RETRY(write(fd, buff, len));
586}
587
588static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
589{
590 memset(pdata, 0, len);
591 pdata->persist_magic = PERSIST_DATA_MAGIC;
592 pdata->persist_valid_entries = 0;
593}
594
595/* A routine to update the passed in crypt_ftr to the lastest version.
596 * fd is open read/write on the device that holds the crypto footer and persistent
597 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
598 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
599 */
600static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
601{
Kenny Root7434b312013-06-14 11:29:53 -0700602 int orig_major = crypt_ftr->major_version;
603 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700604
Kenny Root7434b312013-06-14 11:29:53 -0700605 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
606 struct crypt_persist_data *pdata;
607 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700608
Kenny Rootc4c70f12013-06-14 12:11:38 -0700609 SLOGW("upgrading crypto footer to 1.1");
610
Wei Wang4375f1b2017-02-24 17:43:01 -0800611 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700612 if (pdata == NULL) {
613 SLOGE("Cannot allocate persisent data\n");
614 return;
615 }
616 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
617
618 /* Need to initialize the persistent data area */
619 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
620 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100621 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700622 return;
623 }
624 /* Write all zeros to the first copy, making it invalid */
625 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
626
627 /* Write a valid but empty structure to the second copy */
628 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
629 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
630
631 /* Update the footer */
632 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
633 crypt_ftr->persist_data_offset[0] = pdata_offset;
634 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
635 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100636 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700637 }
638
Paul Lawrencef4faa572014-01-29 13:31:03 -0800639 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700640 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800641 /* But keep the old kdf_type.
642 * It will get updated later to KDF_SCRYPT after the password has been verified.
643 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700644 crypt_ftr->kdf_type = KDF_PBKDF2;
645 get_device_scrypt_params(crypt_ftr);
646 crypt_ftr->minor_version = 2;
647 }
648
Paul Lawrencef4faa572014-01-29 13:31:03 -0800649 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
650 SLOGW("upgrading crypto footer to 1.3");
651 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
652 crypt_ftr->minor_version = 3;
653 }
654
Kenny Root7434b312013-06-14 11:29:53 -0700655 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
656 if (lseek64(fd, offset, SEEK_SET) == -1) {
657 SLOGE("Cannot seek to crypt footer\n");
658 return;
659 }
660 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700661 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700662}
663
664
665static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800666{
667 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800668 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700669 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800670 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700671 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700672 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800673
Ken Sumrall160b4d62013-04-22 12:15:39 -0700674 if (get_crypt_ftr_info(&fname, &starting_off)) {
675 SLOGE("Unable to get crypt_ftr_info\n");
676 return -1;
677 }
678 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700679 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700680 return -1;
681 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700682 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700683 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700684 return -1;
685 }
686
687 /* Make sure it's 16 Kbytes in length */
688 fstat(fd, &statbuf);
689 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
690 SLOGE("footer file %s is not the expected size!\n", fname);
691 goto errout;
692 }
693
694 /* Seek to the start of the crypt footer */
695 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
696 SLOGE("Cannot seek to real block device footer\n");
697 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800698 }
699
700 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
701 SLOGE("Cannot read real block device footer\n");
702 goto errout;
703 }
704
705 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700706 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800707 goto errout;
708 }
709
Kenny Rootc96a5f82013-06-14 12:08:28 -0700710 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
711 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
712 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800713 goto errout;
714 }
715
Greg Kaiser59ad0182018-02-16 13:01:36 -0800716 // We risk buffer overflows with oversized keys, so we just reject them.
717 // 0-sized keys are problematic (essentially by-passing encryption), and
718 // AES-CBC key wrapping only works for multiples of 16 bytes.
719 if ((crypt_ftr->keysize == 0) || ((crypt_ftr->keysize % 16) != 0) ||
720 (crypt_ftr->keysize > MAX_KEY_LEN)) {
721 SLOGE("Invalid keysize (%u) for block device %s; Must be non-zero, "
722 "divisible by 16, and <= %d\n", crypt_ftr->keysize, fname,
723 MAX_KEY_LEN);
724 goto errout;
725 }
726
Kenny Rootc96a5f82013-06-14 12:08:28 -0700727 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
728 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
729 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800730 }
731
Ken Sumrall160b4d62013-04-22 12:15:39 -0700732 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
733 * copy on disk before returning.
734 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700735 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700736 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800737 }
738
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800739 /* Success! */
740 rc = 0;
741
742errout:
743 close(fd);
744 return rc;
745}
746
Ken Sumrall160b4d62013-04-22 12:15:39 -0700747static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
748{
749 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
750 crypt_ftr->persist_data_offset[1]) {
751 SLOGE("Crypt_ftr persist data regions overlap");
752 return -1;
753 }
754
755 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
756 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
757 return -1;
758 }
759
760 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
761 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
762 CRYPT_FOOTER_OFFSET) {
763 SLOGE("Persistent data extends past crypto footer");
764 return -1;
765 }
766
767 return 0;
768}
769
770static int load_persistent_data(void)
771{
772 struct crypt_mnt_ftr crypt_ftr;
773 struct crypt_persist_data *pdata = NULL;
774 char encrypted_state[PROPERTY_VALUE_MAX];
775 char *fname;
776 int found = 0;
777 int fd;
778 int ret;
779 int i;
780
781 if (persist_data) {
782 /* Nothing to do, we've already loaded or initialized it */
783 return 0;
784 }
785
786
787 /* If not encrypted, just allocate an empty table and initialize it */
788 property_get("ro.crypto.state", encrypted_state, "");
789 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800790 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700791 if (pdata) {
792 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
793 persist_data = pdata;
794 return 0;
795 }
796 return -1;
797 }
798
799 if(get_crypt_ftr_and_key(&crypt_ftr)) {
800 return -1;
801 }
802
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700803 if ((crypt_ftr.major_version < 1)
804 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700805 SLOGE("Crypt_ftr version doesn't support persistent data");
806 return -1;
807 }
808
809 if (get_crypt_ftr_info(&fname, NULL)) {
810 return -1;
811 }
812
813 ret = validate_persistent_data_storage(&crypt_ftr);
814 if (ret) {
815 return -1;
816 }
817
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700818 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700819 if (fd < 0) {
820 SLOGE("Cannot open %s metadata file", fname);
821 return -1;
822 }
823
Wei Wang4375f1b2017-02-24 17:43:01 -0800824 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800825 if (pdata == NULL) {
826 SLOGE("Cannot allocate memory for persistent data");
827 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700828 }
829
830 for (i = 0; i < 2; i++) {
831 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
832 SLOGE("Cannot seek to read persistent data on %s", fname);
833 goto err2;
834 }
835 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
836 SLOGE("Error reading persistent data on iteration %d", i);
837 goto err2;
838 }
839 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
840 found = 1;
841 break;
842 }
843 }
844
845 if (!found) {
846 SLOGI("Could not find valid persistent data, creating");
847 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
848 }
849
850 /* Success */
851 persist_data = pdata;
852 close(fd);
853 return 0;
854
855err2:
856 free(pdata);
857
858err:
859 close(fd);
860 return -1;
861}
862
863static int save_persistent_data(void)
864{
865 struct crypt_mnt_ftr crypt_ftr;
866 struct crypt_persist_data *pdata;
867 char *fname;
868 off64_t write_offset;
869 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700870 int fd;
871 int ret;
872
873 if (persist_data == NULL) {
874 SLOGE("No persistent data to save");
875 return -1;
876 }
877
878 if(get_crypt_ftr_and_key(&crypt_ftr)) {
879 return -1;
880 }
881
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700882 if ((crypt_ftr.major_version < 1)
883 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700884 SLOGE("Crypt_ftr version doesn't support persistent data");
885 return -1;
886 }
887
888 ret = validate_persistent_data_storage(&crypt_ftr);
889 if (ret) {
890 return -1;
891 }
892
893 if (get_crypt_ftr_info(&fname, NULL)) {
894 return -1;
895 }
896
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700897 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700898 if (fd < 0) {
899 SLOGE("Cannot open %s metadata file", fname);
900 return -1;
901 }
902
Wei Wang4375f1b2017-02-24 17:43:01 -0800903 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700904 if (pdata == NULL) {
905 SLOGE("Cannot allocate persistant data");
906 goto err;
907 }
908
909 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
910 SLOGE("Cannot seek to read persistent data on %s", fname);
911 goto err2;
912 }
913
914 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
915 SLOGE("Error reading persistent data before save");
916 goto err2;
917 }
918
919 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
920 /* The first copy is the curent valid copy, so write to
921 * the second copy and erase this one */
922 write_offset = crypt_ftr.persist_data_offset[1];
923 erase_offset = crypt_ftr.persist_data_offset[0];
924 } else {
925 /* The second copy must be the valid copy, so write to
926 * the first copy, and erase the second */
927 write_offset = crypt_ftr.persist_data_offset[0];
928 erase_offset = crypt_ftr.persist_data_offset[1];
929 }
930
931 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100932 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700933 SLOGE("Cannot seek to write persistent data");
934 goto err2;
935 }
936 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
937 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100938 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700939 SLOGE("Cannot seek to erase previous persistent data");
940 goto err2;
941 }
942 fsync(fd);
943 memset(pdata, 0, crypt_ftr.persist_data_size);
944 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
945 (int) crypt_ftr.persist_data_size) {
946 SLOGE("Cannot write to erase previous persistent data");
947 goto err2;
948 }
949 fsync(fd);
950 } else {
951 SLOGE("Cannot write to save persistent data");
952 goto err2;
953 }
954
955 /* Success */
956 free(pdata);
957 close(fd);
958 return 0;
959
960err2:
961 free(pdata);
962err:
963 close(fd);
964 return -1;
965}
966
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800967/* Convert a binary key of specified length into an ascii hex string equivalent,
968 * without the leading 0x and with null termination
969 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700970static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700971 unsigned int keysize, char *master_key_ascii) {
972 unsigned int i, a;
973 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800974
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700975 for (i=0, a=0; i<keysize; i++, a+=2) {
976 /* For each byte, write out two ascii hex digits */
977 nibble = (master_key[i] >> 4) & 0xf;
978 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800979
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700980 nibble = master_key[i] & 0xf;
981 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
982 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800983
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700984 /* Add the null termination */
985 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800986
987}
988
Jeff Sharkey9c484982015-03-31 10:35:33 -0700989static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
990 const unsigned char *master_key, const char *real_blk_name,
991 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800992 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800993 struct dm_ioctl *io;
994 struct dm_target_spec *tgt;
995 char *crypt_params;
Greg Kaiser59ad0182018-02-16 13:01:36 -0800996 // We need two ASCII characters to represent each byte, and need space for
997 // the '\0' terminator.
998 char master_key_ascii[MAX_KEY_LEN * 2 + 1];
George Burgess IV605d7ae2016-02-29 13:39:17 -0800999 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001000 int i;
1001
1002 io = (struct dm_ioctl *) buffer;
1003
1004 /* Load the mapping table for this device */
1005 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
1006
1007 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1008 io->target_count = 1;
1009 tgt->status = 0;
1010 tgt->sector_start = 0;
1011 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001012 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001013
1014 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1015 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001016
1017 buff_offset = crypt_params - buffer;
Paul Crowley5afbc622017-11-27 09:42:17 -08001018 SLOGI("Extra parameters for dm_crypt: %s\n", extra_params);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001019 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
1020 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
1021 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001022 crypt_params += strlen(crypt_params) + 1;
1023 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1024 tgt->next = crypt_params - buffer;
1025
1026 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1027 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1028 break;
1029 }
1030 usleep(500000);
1031 }
1032
1033 if (i == TABLE_LOAD_RETRIES) {
1034 /* We failed to load the table, return an error */
1035 return -1;
1036 } else {
1037 return i + 1;
1038 }
1039}
1040
1041
1042static int get_dm_crypt_version(int fd, const char *name, int *version)
1043{
1044 char buffer[DM_CRYPT_BUF_SIZE];
1045 struct dm_ioctl *io;
1046 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001047
1048 io = (struct dm_ioctl *) buffer;
1049
1050 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1051
1052 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1053 return -1;
1054 }
1055
1056 /* Iterate over the returned versions, looking for name of "crypt".
1057 * When found, get and return the version.
1058 */
1059 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1060 while (v->next) {
1061 if (! strcmp(v->name, "crypt")) {
1062 /* We found the crypt driver, return the version, and get out */
1063 version[0] = v->version[0];
1064 version[1] = v->version[1];
1065 version[2] = v->version[2];
1066 return 0;
1067 }
1068 v = (struct dm_target_versions *)(((char *)v) + v->next);
1069 }
1070
1071 return -1;
1072}
1073
Paul Crowley5afbc622017-11-27 09:42:17 -08001074static std::string extra_params_as_string(const std::vector<std::string>& extra_params_vec) {
1075 if (extra_params_vec.empty()) return "";
1076 std::string extra_params = std::to_string(extra_params_vec.size());
1077 for (const auto& p : extra_params_vec) {
1078 extra_params.append(" ");
1079 extra_params.append(p);
1080 }
1081 return extra_params;
1082}
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001083
Paul Crowley5afbc622017-11-27 09:42:17 -08001084static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key,
1085 const char* real_blk_name, char* crypto_blk_name, const char* name,
1086 uint32_t flags) {
1087 char buffer[DM_CRYPT_BUF_SIZE];
1088 struct dm_ioctl* io;
1089 unsigned int minor;
1090 int fd = 0;
1091 int err;
1092 int retval = -1;
1093 int version[3];
1094 int load_count;
1095 std::vector<std::string> extra_params_vec;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001096
Paul Crowley5afbc622017-11-27 09:42:17 -08001097 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
1098 SLOGE("Cannot open device-mapper\n");
1099 goto errout;
1100 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001101
Paul Crowley5afbc622017-11-27 09:42:17 -08001102 io = (struct dm_ioctl*)buffer;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001103
Paul Crowley5afbc622017-11-27 09:42:17 -08001104 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1105 err = ioctl(fd, DM_DEV_CREATE, io);
1106 if (err) {
1107 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
1108 goto errout;
1109 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001110
Paul Crowley5afbc622017-11-27 09:42:17 -08001111 /* Get the device status, in particular, the name of it's device file */
1112 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1113 if (ioctl(fd, DM_DEV_STATUS, io)) {
1114 SLOGE("Cannot retrieve dm-crypt device status\n");
1115 goto errout;
1116 }
1117 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1118 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
Ken Sumralle919efe2012-09-29 17:07:41 -07001119
Paul Crowley5afbc622017-11-27 09:42:17 -08001120 if (!get_dm_crypt_version(fd, name, version)) {
1121 /* Support for allow_discards was added in version 1.11.0 */
1122 if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) {
1123 extra_params_vec.emplace_back("allow_discards");
1124 }
1125 }
1126 if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) {
1127 extra_params_vec.emplace_back("allow_encrypt_override");
1128 }
1129 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd,
1130 extra_params_as_string(extra_params_vec).c_str());
1131 if (load_count < 0) {
1132 SLOGE("Cannot load dm-crypt mapping table.\n");
1133 goto errout;
1134 } else if (load_count > 1) {
1135 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
1136 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001137
Paul Crowley5afbc622017-11-27 09:42:17 -08001138 /* Resume this device to activate it */
1139 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001140
Paul Crowley5afbc622017-11-27 09:42:17 -08001141 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1142 SLOGE("Cannot resume the dm-crypt device\n");
1143 goto errout;
1144 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001145
Paul Crowley5afbc622017-11-27 09:42:17 -08001146 /* We made it here with no errors. Woot! */
1147 retval = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001148
1149errout:
1150 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1151
1152 return retval;
1153}
1154
Wei Wang4375f1b2017-02-24 17:43:01 -08001155static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001156{
1157 int fd;
1158 char buffer[DM_CRYPT_BUF_SIZE];
1159 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001160 int retval = -1;
1161
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001162 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001163 SLOGE("Cannot open device-mapper\n");
1164 goto errout;
1165 }
1166
1167 io = (struct dm_ioctl *) buffer;
1168
1169 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1170 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1171 SLOGE("Cannot remove dm-crypt device\n");
1172 goto errout;
1173 }
1174
1175 /* We made it here with no errors. Woot! */
1176 retval = 0;
1177
1178errout:
1179 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1180
1181 return retval;
1182
1183}
1184
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001185static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001186 unsigned char *ikey, void *params UNUSED)
1187{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001188 SLOGI("Using pbkdf2 for cryptfs KDF");
1189
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001190 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001191 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001192 HASH_COUNT, INTERMEDIATE_BUF_SIZE,
Adam Langleybf0d9722015-11-04 14:51:39 -08001193 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001194}
1195
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001196static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001197 unsigned char *ikey, void *params)
1198{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001199 SLOGI("Using scrypt for cryptfs KDF");
1200
Kenny Rootc4c70f12013-06-14 12:11:38 -07001201 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1202
1203 int N = 1 << ftr->N_factor;
1204 int r = 1 << ftr->r_factor;
1205 int p = 1 << ftr->p_factor;
1206
1207 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001208 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1209 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001210 INTERMEDIATE_BUF_SIZE);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001211
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001212 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001213}
1214
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001215static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1216 unsigned char *ikey, void *params)
1217{
1218 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1219
1220 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001221 size_t signature_size;
1222 unsigned char* signature;
1223 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1224
1225 int N = 1 << ftr->N_factor;
1226 int r = 1 << ftr->r_factor;
1227 int p = 1 << ftr->p_factor;
1228
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001229 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1230 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001231 INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001232
1233 if (rc) {
1234 SLOGE("scrypt failed");
1235 return -1;
1236 }
1237
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001238 if (keymaster_sign_object(ftr, ikey, INTERMEDIATE_BUF_SIZE,
Shawn Willdene17a9c42014-09-08 13:04:08 -06001239 &signature, &signature_size)) {
1240 SLOGE("Signing failed");
1241 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001242 }
1243
1244 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001245 N, r, p, ikey, INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001246 free(signature);
1247
1248 if (rc) {
1249 SLOGE("scrypt failed");
1250 return -1;
1251 }
1252
1253 return 0;
1254}
1255
1256static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1257 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001258 unsigned char *encrypted_master_key,
1259 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001260{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001261 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001262 EVP_CIPHER_CTX e_ctx;
1263 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001264 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001265
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001266 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001267 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001268
1269 switch (crypt_ftr->kdf_type) {
1270 case KDF_SCRYPT_KEYMASTER:
1271 if (keymaster_create_key(crypt_ftr)) {
1272 SLOGE("keymaster_create_key failed");
1273 return -1;
1274 }
1275
1276 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1277 SLOGE("scrypt failed");
1278 return -1;
1279 }
1280 break;
1281
1282 case KDF_SCRYPT:
1283 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1284 SLOGE("scrypt failed");
1285 return -1;
1286 }
1287 break;
1288
1289 default:
1290 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001291 return -1;
1292 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001293
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001295 EVP_CIPHER_CTX_init(&e_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001296 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey,
1297 ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001298 SLOGE("EVP_EncryptInit failed\n");
1299 return -1;
1300 }
1301 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001302
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001303 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001304 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001305 decrypted_master_key, crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001306 SLOGE("EVP_EncryptUpdate failed\n");
1307 return -1;
1308 }
Adam Langley889c4f12014-09-03 14:23:13 -07001309 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001310 SLOGE("EVP_EncryptFinal failed\n");
1311 return -1;
1312 }
1313
Greg Kaiser59ad0182018-02-16 13:01:36 -08001314 if (encrypted_len + final_len != static_cast<int>(crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001315 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1316 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001317 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001318
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001319 /* Store the scrypt of the intermediate key, so we can validate if it's a
1320 password error or mount error when things go wrong.
1321 Note there's no need to check for errors, since if this is incorrect, we
1322 simply won't wipe userdata, which is the correct default behavior
1323 */
1324 int N = 1 << crypt_ftr->N_factor;
1325 int r = 1 << crypt_ftr->r_factor;
1326 int p = 1 << crypt_ftr->p_factor;
1327
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001328 rc = crypto_scrypt(ikey, INTERMEDIATE_KEY_LEN_BYTES,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001329 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1330 crypt_ftr->scrypted_intermediate_key,
1331 sizeof(crypt_ftr->scrypted_intermediate_key));
1332
1333 if (rc) {
1334 SLOGE("encrypt_master_key: crypto_scrypt failed");
1335 }
1336
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001337 EVP_CIPHER_CTX_cleanup(&e_ctx);
1338
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001339 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001340}
1341
Paul Lawrence731a7a22015-04-28 22:14:15 +00001342static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001343 const unsigned char *encrypted_master_key,
1344 size_t keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001345 unsigned char *decrypted_master_key,
1346 kdf_func kdf, void *kdf_params,
1347 unsigned char** intermediate_key,
1348 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001349{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001350 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001351 EVP_CIPHER_CTX d_ctx;
1352 int decrypted_len, final_len;
1353
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001354 /* Turn the password into an intermediate key and IV that can decrypt the
1355 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001356 if (kdf(passwd, salt, ikey, kdf_params)) {
1357 SLOGE("kdf failed");
1358 return -1;
1359 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001360
1361 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001362 EVP_CIPHER_CTX_init(&d_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001363 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364 return -1;
1365 }
1366 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1367 /* Decrypt the master key */
1368 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001369 encrypted_master_key, keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001370 return -1;
1371 }
Adam Langley889c4f12014-09-03 14:23:13 -07001372 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001373 return -1;
1374 }
1375
Greg Kaiser59ad0182018-02-16 13:01:36 -08001376 if (decrypted_len + final_len != static_cast<int>(keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001377 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001378 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001379
1380 /* Copy intermediate key if needed by params */
1381 if (intermediate_key && intermediate_key_size) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001382 *intermediate_key = (unsigned char*) malloc(INTERMEDIATE_KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001383 if (*intermediate_key) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001384 memcpy(*intermediate_key, ikey, INTERMEDIATE_KEY_LEN_BYTES);
1385 *intermediate_key_size = INTERMEDIATE_KEY_LEN_BYTES;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001386 }
1387 }
1388
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001389 EVP_CIPHER_CTX_cleanup(&d_ctx);
1390
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001391 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001392}
1393
Kenny Rootc4c70f12013-06-14 12:11:38 -07001394static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001395{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001396 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001397 *kdf = scrypt_keymaster;
1398 *kdf_params = ftr;
1399 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001400 *kdf = scrypt;
1401 *kdf_params = ftr;
1402 } else {
1403 *kdf = pbkdf2;
1404 *kdf_params = NULL;
1405 }
1406}
1407
Paul Lawrence731a7a22015-04-28 22:14:15 +00001408static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001409 struct crypt_mnt_ftr *crypt_ftr,
1410 unsigned char** intermediate_key,
1411 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001412{
1413 kdf_func kdf;
1414 void *kdf_params;
1415 int ret;
1416
1417 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001418 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001419 crypt_ftr->keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001420 decrypted_master_key, kdf, kdf_params,
1421 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001422 if (ret != 0) {
1423 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001424 }
1425
1426 return ret;
1427}
1428
Wei Wang4375f1b2017-02-24 17:43:01 -08001429static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001430 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001431 int fd;
Greg Kaiser59ad0182018-02-16 13:01:36 -08001432 unsigned char key_buf[MAX_KEY_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001433
1434 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001435 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001436 read(fd, key_buf, sizeof(key_buf));
1437 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001438 close(fd);
1439
1440 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001441 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001442}
1443
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001444int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001445{
Greg Hackmann955653e2014-09-24 14:55:20 -07001446 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001447#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001448
1449 /* Now umount the tmpfs filesystem */
1450 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001451 if (umount(mountpoint) == 0) {
1452 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001453 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001454
1455 if (errno == EINVAL) {
1456 /* EINVAL is returned if the directory is not a mountpoint,
1457 * i.e. there is no filesystem mounted there. So just get out.
1458 */
1459 break;
1460 }
1461
1462 err = errno;
1463
1464 /* If allowed, be increasingly aggressive before the last two retries */
1465 if (kill) {
1466 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1467 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001468 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001469 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1470 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001471 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001472 }
1473 }
1474
1475 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001476 }
1477
1478 if (i < WAIT_UNMOUNT_COUNT) {
1479 SLOGD("unmounting %s succeeded\n", mountpoint);
1480 rc = 0;
1481 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001482 android::vold::KillProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001483 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001484 rc = -1;
1485 }
1486
1487 return rc;
1488}
1489
Wei Wang42e38102017-06-07 10:46:12 -07001490static void prep_data_fs(void)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001491{
Jeff Sharkey47695b22016-02-01 17:02:29 -07001492 // NOTE: post_fs_data results in init calling back around to vold, so all
1493 // callers to this method must be async
1494
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001495 /* Do the prep of the /data filesystem */
1496 property_set("vold.post_fs_data_done", "0");
1497 property_set("vold.decrypt", "trigger_post_fs_data");
Wei Wang42e38102017-06-07 10:46:12 -07001498 SLOGD("Just triggered post_fs_data");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001499
Ken Sumrallc5872692013-05-14 15:26:31 -07001500 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang42e38102017-06-07 10:46:12 -07001501 while (!android::base::WaitForProperty("vold.post_fs_data_done",
Wei Wang4375f1b2017-02-24 17:43:01 -08001502 "1",
Wei Wang42e38102017-06-07 10:46:12 -07001503 std::chrono::seconds(15))) {
1504 /* We timed out to prep /data in time. Continue wait. */
1505 SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001506 }
Wei Wang42e38102017-06-07 10:46:12 -07001507 SLOGD("post_fs_data done");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001508}
1509
Paul Lawrence74f29f12014-08-28 15:54:10 -07001510static void cryptfs_set_corrupt()
1511{
1512 // Mark the footer as bad
1513 struct crypt_mnt_ftr crypt_ftr;
1514 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1515 SLOGE("Failed to get crypto footer - panic");
1516 return;
1517 }
1518
1519 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1520 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1521 SLOGE("Failed to set crypto footer - panic");
1522 return;
1523 }
1524}
1525
1526static void cryptfs_trigger_restart_min_framework()
1527{
1528 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1529 SLOGE("Failed to mount tmpfs on data - panic");
1530 return;
1531 }
1532
1533 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1534 SLOGE("Failed to trigger post fs data - panic");
1535 return;
1536 }
1537
1538 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1539 SLOGE("Failed to trigger restart min framework - panic");
1540 return;
1541 }
1542}
1543
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001544/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001545static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001546{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001547 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001548 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001549 static int restart_successful = 0;
1550
1551 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001552 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001553 SLOGE("Encrypted filesystem not validated, aborting");
1554 return -1;
1555 }
1556
1557 if (restart_successful) {
1558 SLOGE("System already restarted with encrypted disk, aborting");
1559 return -1;
1560 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001561
Paul Lawrencef4faa572014-01-29 13:31:03 -08001562 if (restart_main) {
1563 /* Here is where we shut down the framework. The init scripts
1564 * start all services in one of three classes: core, main or late_start.
1565 * On boot, we start core and main. Now, we stop main, but not core,
1566 * as core includes vold and a few other really important things that
1567 * we need to keep running. Once main has stopped, we should be able
1568 * to umount the tmpfs /data, then mount the encrypted /data.
1569 * We then restart the class main, and also the class late_start.
1570 * At the moment, I've only put a few things in late_start that I know
1571 * are not needed to bring up the framework, and that also cause problems
1572 * with unmounting the tmpfs /data, but I hope to add add more services
1573 * to the late_start class as we optimize this to decrease the delay
1574 * till the user is asked for the password to the filesystem.
1575 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001576
Paul Lawrencef4faa572014-01-29 13:31:03 -08001577 /* The init files are setup to stop the class main when vold.decrypt is
1578 * set to trigger_reset_main.
1579 */
1580 property_set("vold.decrypt", "trigger_reset_main");
1581 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001582
Paul Lawrencef4faa572014-01-29 13:31:03 -08001583 /* Ugh, shutting down the framework is not synchronous, so until it
1584 * can be fixed, this horrible hack will wait a moment for it all to
1585 * shut down before proceeding. Without it, some devices cannot
1586 * restart the graphics services.
1587 */
1588 sleep(2);
1589 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001590
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001591 /* Now that the framework is shutdown, we should be able to umount()
1592 * the tmpfs filesystem, and mount the real one.
1593 */
1594
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001595 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1596 if (strlen(crypto_blkdev) == 0) {
1597 SLOGE("fs_crypto_blkdev not set\n");
1598 return -1;
1599 }
1600
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001601 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001602 /* If ro.crypto.readonly is set to 1, mount the decrypted
1603 * filesystem readonly. This is used when /data is mounted by
1604 * recovery mode.
1605 */
1606 char ro_prop[PROPERTY_VALUE_MAX];
1607 property_get("ro.crypto.readonly", ro_prop, "");
Jeff Sharkey95440eb2017-09-18 18:19:28 -06001608 if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001609 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07001610 if (rec) {
1611 rec->flags |= MS_RDONLY;
1612 }
Doug Zongker6fd57712013-12-17 09:43:23 -08001613 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001614
Ken Sumralle5032c42012-04-01 23:58:44 -07001615 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001616 int retries = RETRY_MOUNT_ATTEMPTS;
1617 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001618
1619 /*
1620 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1621 * partitions in the fsck domain.
1622 */
1623 if (setexeccon(secontextFsck())){
1624 SLOGE("Failed to setexeccon");
1625 return -1;
1626 }
Paul Crowleye2ee1522017-09-26 14:05:26 -07001627 while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT,
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001628 crypto_blkdev, 0))
1629 != 0) {
1630 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1631 /* TODO: invoke something similar to
1632 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1633 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1634 SLOGI("Failed to mount %s because it is busy - waiting",
1635 crypto_blkdev);
1636 if (--retries) {
1637 sleep(RETRY_MOUNT_DELAY_SECONDS);
1638 } else {
1639 /* Let's hope that a reboot clears away whatever is keeping
1640 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001641 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001642 }
1643 } else {
1644 SLOGE("Failed to mount decrypted data");
1645 cryptfs_set_corrupt();
1646 cryptfs_trigger_restart_min_framework();
1647 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001648 if (setexeccon(NULL)) {
1649 SLOGE("Failed to setexeccon");
1650 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001651 return -1;
1652 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001653 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001654 if (setexeccon(NULL)) {
1655 SLOGE("Failed to setexeccon");
1656 return -1;
1657 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001658
Ken Sumralle5032c42012-04-01 23:58:44 -07001659 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07001660 prep_data_fs();
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001661 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001662
1663 /* startup service classes main and late_start */
1664 property_set("vold.decrypt", "trigger_restart_framework");
1665 SLOGD("Just triggered restart_framework\n");
1666
1667 /* Give it a few moments to get started */
1668 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001669 }
1670
Ken Sumrall0cc16632011-01-18 20:32:26 -08001671 if (rc == 0) {
1672 restart_successful = 1;
1673 }
1674
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001675 return rc;
1676}
1677
Paul Lawrencef4faa572014-01-29 13:31:03 -08001678int cryptfs_restart(void)
1679{
Paul Lawrence05335c32015-03-05 09:46:23 -08001680 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001681 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001682 SLOGE("cryptfs_restart not valid for file encryption:");
1683 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001684 }
1685
Paul Lawrencef4faa572014-01-29 13:31:03 -08001686 /* Call internal implementation forcing a restart of main service group */
1687 return cryptfs_restart_internal(1);
1688}
1689
Wei Wang4375f1b2017-02-24 17:43:01 -08001690static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001691{
1692 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001693 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001694 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001695
1696 property_get("ro.crypto.state", encrypted_state, "");
1697 if (strcmp(encrypted_state, "encrypted") ) {
1698 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001699 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001700 }
1701
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001702 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001703 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001704 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001705 }
1706
Ken Sumrall160b4d62013-04-22 12:15:39 -07001707 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001708 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001709
Ken Sumralle1a45852011-12-14 21:24:27 -08001710 /*
1711 * Only report this error if key_loc is a file and it exists.
1712 * If the device was never encrypted, and /data is not mountable for
1713 * some reason, returning 1 should prevent the UI from presenting the
1714 * a "enter password" screen, or worse, a "press button to wipe the
1715 * device" screen.
1716 */
1717 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1718 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001719 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001720 } else {
1721 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001722 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001723 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001724 }
1725
Paul Lawrence74f29f12014-08-28 15:54:10 -07001726 // Test for possible error flags
1727 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1728 SLOGE("Encryption process is partway completed\n");
1729 return CRYPTO_COMPLETE_PARTIAL;
1730 }
1731
1732 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1733 SLOGE("Encryption process was interrupted but cannot continue\n");
1734 return CRYPTO_COMPLETE_INCONSISTENT;
1735 }
1736
1737 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1738 SLOGE("Encryption is successful but data is corrupt\n");
1739 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001740 }
1741
1742 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001743 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001744}
1745
Paul Lawrencef4faa572014-01-29 13:31:03 -08001746static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001747 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001748{
Greg Kaiser59ad0182018-02-16 13:01:36 -08001749 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001750 char crypto_blkdev[MAXPATHLEN];
1751 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001752 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001753 unsigned int orig_failed_decrypt_count;
1754 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001755 int use_keymaster = 0;
1756 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001757 unsigned char* intermediate_key = 0;
1758 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001759 int N = 1 << crypt_ftr->N_factor;
1760 int r = 1 << crypt_ftr->r_factor;
1761 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001762
Paul Lawrencef4faa572014-01-29 13:31:03 -08001763 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1764 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001765
Paul Lawrencef4faa572014-01-29 13:31:03 -08001766 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001767 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1768 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001769 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001770 rc = -1;
1771 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001772 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001773 }
1774
Paul Crowleye2ee1522017-09-26 14:05:26 -07001775 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Paul Lawrencef4faa572014-01-29 13:31:03 -08001776
Paul Lawrence74f29f12014-08-28 15:54:10 -07001777 // Create crypto block device - all (non fatal) code paths
1778 // need it
Paul Crowley5afbc622017-11-27 09:42:17 -08001779 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, label, 0)) {
1780 SLOGE("Error creating decrypted block device\n");
1781 rc = -1;
1782 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001783 }
1784
Paul Lawrence74f29f12014-08-28 15:54:10 -07001785 /* Work out if the problem is the password or the data */
1786 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1787 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001788
Paul Lawrence74f29f12014-08-28 15:54:10 -07001789 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1790 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1791 N, r, p, scrypted_intermediate_key,
1792 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001793
Paul Lawrence74f29f12014-08-28 15:54:10 -07001794 // Does the key match the crypto footer?
1795 if (rc == 0 && memcmp(scrypted_intermediate_key,
1796 crypt_ftr->scrypted_intermediate_key,
1797 sizeof(scrypted_intermediate_key)) == 0) {
1798 SLOGI("Password matches");
1799 rc = 0;
1800 } else {
1801 /* Try mounting the file system anyway, just in case the problem's with
1802 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001803 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1804 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001805 mkdir(tmp_mount_point, 0755);
Paul Crowleye2ee1522017-09-26 14:05:26 -07001806 if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001807 SLOGE("Error temp mounting decrypted block device\n");
1808 delete_crypto_blk_dev(label);
1809
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001810 rc = ++crypt_ftr->failed_decrypt_count;
1811 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001812 } else {
1813 /* Success! */
1814 SLOGI("Password did not match but decrypted drive mounted - continue");
1815 umount(tmp_mount_point);
1816 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001817 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001818 }
1819
1820 if (rc == 0) {
1821 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001822 if (orig_failed_decrypt_count != 0) {
1823 put_crypt_ftr_and_key(crypt_ftr);
1824 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001825
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001826 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001827 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001828 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001829
1830 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001831 * the key when we want to change the password on it. */
Greg Kaiser59ad0182018-02-16 13:01:36 -08001832 memcpy(saved_master_key, decrypted_master_key, crypt_ftr->keysize);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001833 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001834 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001835 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001836 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001837
Paul Lawrence74f29f12014-08-28 15:54:10 -07001838 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001839 use_keymaster = keymaster_check_compatibility();
1840 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001841 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001842 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1843 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1844 upgrade = 1;
1845 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001846 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001847 upgrade = 1;
1848 }
1849
1850 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001851 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1852 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001853 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001854 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001855 }
1856 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001857
1858 // Do not fail even if upgrade failed - machine is bootable
1859 // Note that if this code is ever hit, there is a *serious* problem
1860 // since KDFs should never fail. You *must* fix the kdf before
1861 // proceeding!
1862 if (rc) {
1863 SLOGW("Upgrade failed with error %d,"
1864 " but continuing with previous state",
1865 rc);
1866 rc = 0;
1867 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001868 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001869 }
1870
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001871 errout:
1872 if (intermediate_key) {
1873 memset(intermediate_key, 0, intermediate_key_size);
1874 free(intermediate_key);
1875 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001876 return rc;
1877}
1878
Ken Sumrall29d8da82011-05-18 17:20:07 -07001879/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001880 * Called by vold when it's asked to mount an encrypted external
1881 * storage volume. The incoming partition has no crypto header/footer,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001882 * as any metadata is been stored in a separate, small partition. We
1883 * assume it must be using our same crypt type and keysize.
Jeff Sharkey9c484982015-03-31 10:35:33 -07001884 *
1885 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001886 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001887int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001888 const unsigned char* key, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001889 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001890 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001891 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001892 return -1;
1893 }
1894
1895 unsigned long nr_sec = 0;
1896 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001897 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001898
Ken Sumrall29d8da82011-05-18 17:20:07 -07001899 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001900 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001901 return -1;
1902 }
1903
Jeff Sharkey9c484982015-03-31 10:35:33 -07001904 struct crypt_mnt_ftr ext_crypt_ftr;
1905 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1906 ext_crypt_ftr.fs_size = nr_sec;
Greg Kaiser57f9af62018-02-16 13:13:58 -08001907 ext_crypt_ftr.keysize = cryptfs_get_keysize();
1908 strlcpy((char*) ext_crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(),
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001909 MAX_CRYPTO_TYPE_NAME_LEN);
Paul Crowley385cb8c2018-03-29 13:27:23 -07001910 uint32_t flags = 0;
1911 if (e4crypt_is_native() &&
1912 android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false))
1913 flags |= CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001914
Paul Crowley385cb8c2018-03-29 13:27:23 -07001915 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, out_crypto_blkdev, label, flags);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001916}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001917
Jeff Sharkey9c484982015-03-31 10:35:33 -07001918/*
1919 * Called by vold when it's asked to unmount an encrypted external
1920 * storage volume.
1921 */
1922int cryptfs_revert_ext_volume(const char* label) {
1923 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001924}
1925
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001926int cryptfs_crypto_complete(void)
1927{
1928 return do_crypto_complete("/data");
1929}
1930
Paul Lawrencef4faa572014-01-29 13:31:03 -08001931int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1932{
1933 char encrypted_state[PROPERTY_VALUE_MAX];
1934 property_get("ro.crypto.state", encrypted_state, "");
1935 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1936 SLOGE("encrypted fs already validated or not running with encryption,"
1937 " aborting");
1938 return -1;
1939 }
1940
1941 if (get_crypt_ftr_and_key(crypt_ftr)) {
1942 SLOGE("Error getting crypt footer and key");
1943 return -1;
1944 }
1945
1946 return 0;
1947}
1948
Wei Wang4375f1b2017-02-24 17:43:01 -08001949int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001950{
Paul Lawrence05335c32015-03-05 09:46:23 -08001951 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001952 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001953 SLOGE("cryptfs_check_passwd not valid for file encryption");
1954 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001955 }
1956
Paul Lawrencef4faa572014-01-29 13:31:03 -08001957 struct crypt_mnt_ftr crypt_ftr;
1958 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001959
Paul Lawrencef4faa572014-01-29 13:31:03 -08001960 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001961 if (rc) {
1962 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001963 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001964 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001965
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001966 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001967 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1968 if (rc) {
1969 SLOGE("Password did not match");
1970 return rc;
1971 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001972
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001973 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1974 // Here we have a default actual password but a real password
1975 // we must test against the scrypted value
1976 // First, we must delete the crypto block device that
1977 // test_mount_encrypted_fs leaves behind as a side effect
1978 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1979 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1980 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1981 if (rc) {
1982 SLOGE("Default password did not match on reboot encryption");
1983 return rc;
1984 }
1985
1986 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1987 put_crypt_ftr_and_key(&crypt_ftr);
1988 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1989 if (rc) {
1990 SLOGE("Could not change password on reboot encryption");
1991 return rc;
1992 }
1993 }
1994
1995 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001996 cryptfs_clear_password();
1997 password = strdup(passwd);
1998 struct timespec now;
1999 clock_gettime(CLOCK_BOOTTIME, &now);
2000 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002001 }
2002
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002003 return rc;
2004}
2005
Jeff Sharkey83b559c2017-09-12 16:30:52 -06002006int cryptfs_verify_passwd(const char *passwd)
Ken Sumrall3ad90722011-10-04 20:38:29 -07002007{
2008 struct crypt_mnt_ftr crypt_ftr;
Greg Kaiser59ad0182018-02-16 13:01:36 -08002009 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002010 char encrypted_state[PROPERTY_VALUE_MAX];
2011 int rc;
2012
2013 property_get("ro.crypto.state", encrypted_state, "");
2014 if (strcmp(encrypted_state, "encrypted") ) {
2015 SLOGE("device not encrypted, aborting");
2016 return -2;
2017 }
2018
2019 if (!master_key_saved) {
2020 SLOGE("encrypted fs not yet mounted, aborting");
2021 return -1;
2022 }
2023
2024 if (!saved_mount_point) {
2025 SLOGE("encrypted fs failed to save mount point, aborting");
2026 return -1;
2027 }
2028
Ken Sumrall160b4d62013-04-22 12:15:39 -07002029 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002030 SLOGE("Error getting crypt footer and key\n");
2031 return -1;
2032 }
2033
2034 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2035 /* If the device has no password, then just say the password is valid */
2036 rc = 0;
2037 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002038 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002039 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2040 /* They match, the password is correct */
2041 rc = 0;
2042 } else {
2043 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2044 sleep(1);
2045 rc = 1;
2046 }
2047 }
2048
2049 return rc;
2050}
2051
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002052/* Initialize a crypt_mnt_ftr structure. The keysize is
Greg Kaiser57f9af62018-02-16 13:13:58 -08002053 * defaulted to cryptfs_get_keysize() bytes, and the filesystem size to 0.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002054 * Presumably, at a minimum, the caller will update the
2055 * filesystem size and crypto_type_name after calling this function.
2056 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002057static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002058{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002059 off64_t off;
2060
2061 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002062 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002063 ftr->major_version = CURRENT_MAJOR_VERSION;
2064 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002065 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Greg Kaiser57f9af62018-02-16 13:13:58 -08002066 ftr->keysize = cryptfs_get_keysize();
Ken Sumrall160b4d62013-04-22 12:15:39 -07002067
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002068 switch (keymaster_check_compatibility()) {
2069 case 1:
2070 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2071 break;
2072
2073 case 0:
2074 ftr->kdf_type = KDF_SCRYPT;
2075 break;
2076
2077 default:
2078 SLOGE("keymaster_check_compatibility failed");
2079 return -1;
2080 }
2081
Kenny Rootc4c70f12013-06-14 12:11:38 -07002082 get_device_scrypt_params(ftr);
2083
Ken Sumrall160b4d62013-04-22 12:15:39 -07002084 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2085 if (get_crypt_ftr_info(NULL, &off) == 0) {
2086 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2087 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2088 ftr->persist_data_size;
2089 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002090
2091 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002092}
2093
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002094#define FRAMEWORK_BOOT_WAIT 60
2095
Paul Lawrence87999172014-02-20 12:21:31 -08002096static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2097{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002098 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002099 if (fd == -1) {
2100 SLOGE("Error opening file %s", filename);
2101 return -1;
2102 }
2103
2104 char block[CRYPT_INPLACE_BUFSIZE];
2105 memset(block, 0, sizeof(block));
2106 if (unix_read(fd, block, sizeof(block)) < 0) {
2107 SLOGE("Error reading file %s", filename);
2108 close(fd);
2109 return -1;
2110 }
2111
2112 close(fd);
2113
2114 SHA256_CTX c;
2115 SHA256_Init(&c);
2116 SHA256_Update(&c, block, sizeof(block));
2117 SHA256_Final(buf, &c);
2118
2119 return 0;
2120}
2121
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002122static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr* crypt_ftr, char* crypto_blkdev,
2123 char* real_blkdev, int previously_encrypted_upto) {
Paul Lawrence87999172014-02-20 12:21:31 -08002124 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002125 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002126
Paul Lawrence87999172014-02-20 12:21:31 -08002127 /* The size of the userdata partition, and add in the vold volumes below */
2128 tot_encryption_size = crypt_ftr->fs_size;
2129
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002130 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr->fs_size, &cur_encryption_done,
Paul Crowley0fd26262018-01-30 09:48:19 -08002131 tot_encryption_size, previously_encrypted_upto, true);
Paul Lawrence87999172014-02-20 12:21:31 -08002132
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002133 if (rc == ENABLE_INPLACE_ERR_DEV) {
2134 /* Hack for b/17898962 */
2135 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2136 cryptfs_reboot(RebootType::reboot);
2137 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002138
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002139 if (!rc) {
2140 crypt_ftr->encrypted_upto = cur_encryption_done;
2141 }
Paul Lawrence87999172014-02-20 12:21:31 -08002142
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002143 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
2144 /* The inplace routine never actually sets the progress to 100% due
2145 * to the round down nature of integer division, so set it here */
2146 property_set("vold.encrypt_progress", "100");
Paul Lawrence87999172014-02-20 12:21:31 -08002147 }
2148
2149 return rc;
2150}
2151
Paul Crowleyb64933a2017-10-31 08:25:55 -07002152static int vold_unmountAll(void) {
2153 VolumeManager* vm = VolumeManager::Instance();
2154 return vm->unmountAll();
2155}
2156
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002157int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) {
Paul Lawrence87999172014-02-20 12:21:31 -08002158 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Greg Kaiser59ad0182018-02-16 13:01:36 -08002159 unsigned char decrypted_master_key[MAX_KEY_LEN];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002160 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002161 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002162 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002163 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002164 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002165 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002166 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002167 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002168 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002169 bool onlyCreateHeader = false;
2170 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002171
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002172 if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002173 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2174 /* An encryption was underway and was interrupted */
2175 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2176 crypt_ftr.encrypted_upto = 0;
2177 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002178
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002179 /* At this point, we are in an inconsistent state. Until we successfully
2180 complete encryption, a reboot will leave us broken. So mark the
2181 encryption failed in case that happens.
2182 On successfully completing encryption, remove this flag */
2183 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002184
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002185 put_crypt_ftr_and_key(&crypt_ftr);
2186 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2187 if (!check_ftr_sha(&crypt_ftr)) {
2188 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2189 put_crypt_ftr_and_key(&crypt_ftr);
2190 goto error_unencrypted;
2191 }
2192
2193 /* Doing a reboot-encryption*/
2194 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2195 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2196 rebootEncryption = true;
2197 }
Greg Kaiser59ad0182018-02-16 13:01:36 -08002198 } else {
2199 // We don't want to accidentally reference invalid data.
2200 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
Paul Lawrence87999172014-02-20 12:21:31 -08002201 }
2202
2203 property_get("ro.crypto.state", encrypted_state, "");
2204 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2205 SLOGE("Device is already running encrypted, aborting");
2206 goto error_unencrypted;
2207 }
2208
2209 // TODO refactor fs_mgr_get_crypt_info to get both in one call
Paul Crowleye2ee1522017-09-26 14:05:26 -07002210 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
2211 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002212
Ken Sumrall3ed82362011-01-28 23:31:16 -08002213 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002214 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002215 if (fd == -1) {
2216 SLOGE("Cannot open block device %s\n", real_blkdev);
2217 goto error_unencrypted;
2218 }
2219 unsigned long nr_sec;
2220 get_blkdev_size(fd, &nr_sec);
2221 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002222 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2223 goto error_unencrypted;
2224 }
2225 close(fd);
2226
2227 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002228 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002229 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002230 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002231 if (fs_size_sec == 0)
2232 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2233
Paul Lawrence87999172014-02-20 12:21:31 -08002234 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002235
2236 if (fs_size_sec > max_fs_size_sec) {
2237 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2238 goto error_unencrypted;
2239 }
2240 }
2241
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002242 /* Get a wakelock as this may take a while, and we don't want the
2243 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2244 * wants to keep the screen on, it can grab a full wakelock.
2245 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002246 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002247 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2248
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002249 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002250 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002251 */
2252 property_set("vold.decrypt", "trigger_shutdown_framework");
2253 SLOGD("Just asked init to shut down class main\n");
2254
Jeff Sharkey9c484982015-03-31 10:35:33 -07002255 /* Ask vold to unmount all devices that it manages */
2256 if (vold_unmountAll()) {
2257 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002258 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002259
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002260 /* no_ui means we are being called from init, not settings.
2261 Now we always reboot from settings, so !no_ui means reboot
2262 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002263 if (!no_ui) {
2264 /* Try fallback, which is to reboot and try there */
2265 onlyCreateHeader = true;
2266 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2267 if (breadcrumb == 0) {
2268 SLOGE("Failed to create breadcrumb file");
2269 goto error_shutting_down;
2270 }
2271 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002272 }
2273
2274 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002275 if (!onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002276 /* Now that /data is unmounted, we need to mount a tmpfs
2277 * /data, set a property saying we're doing inplace encryption,
2278 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002279 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002280 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002281 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002282 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002283 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002284 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002285
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002286 /* restart the framework. */
2287 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07002288 prep_data_fs();
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002289
Ken Sumrall92736ef2012-10-17 20:57:14 -07002290 /* Ugh, shutting down the framework is not synchronous, so until it
2291 * can be fixed, this horrible hack will wait a moment for it all to
2292 * shut down before proceeding. Without it, some devices cannot
2293 * restart the graphics services.
2294 */
2295 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002296 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002297
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002298 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002299 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002300 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002301 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2302 goto error_shutting_down;
2303 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002304
Paul Lawrence87999172014-02-20 12:21:31 -08002305 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2306 crypt_ftr.fs_size = nr_sec
2307 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2308 } else {
2309 crypt_ftr.fs_size = nr_sec;
2310 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002311 /* At this point, we are in an inconsistent state. Until we successfully
2312 complete encryption, a reboot will leave us broken. So mark the
2313 encryption failed in case that happens.
2314 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002315 if (onlyCreateHeader) {
2316 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2317 } else {
2318 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2319 }
Paul Lawrence87999172014-02-20 12:21:31 -08002320 crypt_ftr.crypt_type = crypt_type;
Greg Kaiser57f9af62018-02-16 13:13:58 -08002321 strlcpy((char *)crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(), MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002322
Paul Lawrence87999172014-02-20 12:21:31 -08002323 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002324 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2325 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002326 SLOGE("Cannot create encrypted master key\n");
2327 goto error_shutting_down;
2328 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002329
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002330 /* Replace scrypted intermediate key if we are preparing for a reboot */
2331 if (onlyCreateHeader) {
Greg Kaiser59ad0182018-02-16 13:01:36 -08002332 unsigned char fake_master_key[MAX_KEY_LEN];
2333 unsigned char encrypted_fake_master_key[MAX_KEY_LEN];
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002334 memset(fake_master_key, 0, sizeof(fake_master_key));
2335 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2336 encrypted_fake_master_key, &crypt_ftr);
2337 }
2338
Paul Lawrence87999172014-02-20 12:21:31 -08002339 /* Write the key to the end of the partition */
2340 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002341
Paul Lawrence87999172014-02-20 12:21:31 -08002342 /* If any persistent data has been remembered, save it.
2343 * If none, create a valid empty table and save that.
2344 */
2345 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002346 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002347 if (pdata) {
2348 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2349 persist_data = pdata;
2350 }
2351 }
2352 if (persist_data) {
2353 save_persistent_data();
2354 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002355 }
2356
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002357 if (onlyCreateHeader) {
2358 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002359 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002360 }
2361
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002362 if (!no_ui || rebootEncryption) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002363 /* startup service classes main and late_start */
2364 property_set("vold.decrypt", "trigger_restart_min_framework");
2365 SLOGD("Just triggered restart_min_framework\n");
2366
2367 /* OK, the framework is restarted and will soon be showing a
2368 * progress bar. Time to setup an encrypted mapping, and
2369 * either write a new filesystem, or encrypt in place updating
2370 * the progress bar as we work.
2371 */
2372 }
2373
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002374 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002375 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Crowley5afbc622017-11-27 09:42:17 -08002376 CRYPTO_BLOCK_DEVICE, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002377
Paul Lawrence87999172014-02-20 12:21:31 -08002378 /* If we are continuing, check checksums match */
2379 rc = 0;
2380 if (previously_encrypted_upto) {
2381 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2382 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002383
Paul Lawrence87999172014-02-20 12:21:31 -08002384 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2385 sizeof(hash_first_block)) != 0) {
2386 SLOGE("Checksums do not match - trigger wipe");
2387 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002388 }
2389 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002390
Paul Lawrence87999172014-02-20 12:21:31 -08002391 if (!rc) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002392 rc = cryptfs_enable_all_volumes(&crypt_ftr, crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002393 previously_encrypted_upto);
2394 }
2395
2396 /* Calculate checksum if we are not finished */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002397 if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002398 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2399 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002400 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002401 SLOGE("Error calculating checksum for continuing encryption");
2402 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002403 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002404 }
2405
2406 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002407 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002408
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002409 if (! rc) {
2410 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002411 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002412
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002413 if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002414 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2415 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002416 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002417 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002418
Paul Lawrence6bfed202014-07-28 12:47:22 -07002419 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002420
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002421 if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
2422 char value[PROPERTY_VALUE_MAX];
2423 property_get("ro.crypto.state", value, "");
2424 if (!strcmp(value, "")) {
2425 /* default encryption - continue first boot sequence */
2426 property_set("ro.crypto.state", "encrypted");
2427 property_set("ro.crypto.type", "block");
2428 release_wake_lock(lockid);
2429 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2430 // Bring up cryptkeeper that will check the password and set it
2431 property_set("vold.decrypt", "trigger_shutdown_framework");
2432 sleep(2);
2433 property_set("vold.encrypt_progress", "");
2434 cryptfs_trigger_restart_min_framework();
2435 } else {
2436 cryptfs_check_passwd(DEFAULT_PASSWORD);
2437 cryptfs_restart_internal(1);
2438 }
2439 return 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002440 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002441 sleep(2); /* Give the UI a chance to show 100% progress */
2442 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002443 }
Paul Lawrence87999172014-02-20 12:21:31 -08002444 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002445 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002446 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002447 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002448 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002449 char value[PROPERTY_VALUE_MAX];
2450
Ken Sumrall319369a2012-06-27 16:30:18 -07002451 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002452 if (!strcmp(value, "1")) {
2453 /* wipe data if encryption failed */
2454 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002455 std::string err;
2456 const std::vector<std::string> options = {
2457 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2458 };
2459 if (!write_bootloader_message(options, &err)) {
2460 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002461 }
Josh Gaofec44372017-08-28 13:22:55 -07002462 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002463 } else {
2464 /* set property to trigger dialog */
2465 property_set("vold.encrypt_progress", "error_partially_encrypted");
2466 release_wake_lock(lockid);
2467 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002468 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002469 }
2470
Ken Sumrall3ed82362011-01-28 23:31:16 -08002471 /* hrm, the encrypt step claims success, but the reboot failed.
2472 * This should not happen.
2473 * Set the property and return. Hope the framework can deal with it.
2474 */
2475 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002476 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002477 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002478
2479error_unencrypted:
2480 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002481 if (lockid[0]) {
2482 release_wake_lock(lockid);
2483 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002484 return -1;
2485
2486error_shutting_down:
2487 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2488 * but the framework is stopped and not restarted to show the error, so it's up to
2489 * vold to restart the system.
2490 */
2491 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002492 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002493
2494 /* shouldn't get here */
2495 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002496 if (lockid[0]) {
2497 release_wake_lock(lockid);
2498 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002499 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002500}
2501
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002502int cryptfs_enable(int type, const char* passwd, int no_ui) {
2503 return cryptfs_enable_internal(type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002504}
2505
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002506int cryptfs_enable_default(int no_ui) {
2507 return cryptfs_enable_internal(CRYPT_TYPE_DEFAULT, DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002508}
2509
2510int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002511{
Paul Crowley38132a12016-02-09 09:50:32 +00002512 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002513 SLOGE("cryptfs_changepw not valid for file encryption");
2514 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002515 }
2516
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002517 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002518 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002519
2520 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002521 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002522 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002523 return -1;
2524 }
2525
Paul Lawrencef4faa572014-01-29 13:31:03 -08002526 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2527 SLOGE("Invalid crypt_type %d", crypt_type);
2528 return -1;
2529 }
2530
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002531 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002532 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002533 SLOGE("Error getting crypt footer and key");
2534 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002535 }
2536
Paul Lawrencef4faa572014-01-29 13:31:03 -08002537 crypt_ftr.crypt_type = crypt_type;
2538
JP Abgrall933216c2015-02-11 13:44:32 -08002539 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002540 : newpw,
2541 crypt_ftr.salt,
2542 saved_master_key,
2543 crypt_ftr.master_key,
2544 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002545 if (rc) {
2546 SLOGE("Encrypt master key failed: %d", rc);
2547 return -1;
2548 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002549 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002550 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002551
2552 return 0;
2553}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002554
Rubin Xu85c01f92014-10-13 12:49:54 +01002555static unsigned int persist_get_max_entries(int encrypted) {
2556 struct crypt_mnt_ftr crypt_ftr;
2557 unsigned int dsize;
2558 unsigned int max_persistent_entries;
2559
2560 /* If encrypted, use the values from the crypt_ftr, otherwise
2561 * use the values for the current spec.
2562 */
2563 if (encrypted) {
2564 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2565 return -1;
2566 }
2567 dsize = crypt_ftr.persist_data_size;
2568 } else {
2569 dsize = CRYPT_PERSIST_DATA_SIZE;
2570 }
2571
2572 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2573 sizeof(struct crypt_persist_entry);
2574
2575 return max_persistent_entries;
2576}
2577
2578static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002579{
2580 unsigned int i;
2581
2582 if (persist_data == NULL) {
2583 return -1;
2584 }
2585 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2586 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2587 /* We found it! */
2588 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2589 return 0;
2590 }
2591 }
2592
2593 return -1;
2594}
2595
Rubin Xu85c01f92014-10-13 12:49:54 +01002596static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002597{
2598 unsigned int i;
2599 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002600 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002601
2602 if (persist_data == NULL) {
2603 return -1;
2604 }
2605
Rubin Xu85c01f92014-10-13 12:49:54 +01002606 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002607
2608 num = persist_data->persist_valid_entries;
2609
2610 for (i = 0; i < num; i++) {
2611 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2612 /* We found an existing entry, update it! */
2613 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2614 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2615 return 0;
2616 }
2617 }
2618
2619 /* We didn't find it, add it to the end, if there is room */
2620 if (persist_data->persist_valid_entries < max_persistent_entries) {
2621 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2622 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2623 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2624 persist_data->persist_valid_entries++;
2625 return 0;
2626 }
2627
2628 return -1;
2629}
2630
Rubin Xu85c01f92014-10-13 12:49:54 +01002631/**
2632 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2633 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2634 */
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002635int match_multi_entry(const char *key, const char *field, unsigned index) {
2636 std::string key_ = key;
2637 std::string field_ = field;
Rubin Xu85c01f92014-10-13 12:49:54 +01002638
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002639 std::string parsed_field;
2640 unsigned parsed_index;
2641
2642 std::string::size_type split = key_.find_last_of('_');
2643 if (split == std::string::npos) {
2644 parsed_field = key_;
2645 parsed_index = 0;
2646 } else {
2647 parsed_field = key_.substr(0, split);
2648 parsed_index = std::stoi(key_.substr(split + 1));
Rubin Xu85c01f92014-10-13 12:49:54 +01002649 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002650
2651 return parsed_field == field_ && parsed_index >= index;
Rubin Xu85c01f92014-10-13 12:49:54 +01002652}
2653
2654/*
2655 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2656 * remaining entries starting from index will be deleted.
2657 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2658 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2659 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2660 *
2661 */
2662static int persist_del_keys(const char *fieldname, unsigned index)
2663{
2664 unsigned int i;
2665 unsigned int j;
2666 unsigned int num;
2667
2668 if (persist_data == NULL) {
2669 return PERSIST_DEL_KEY_ERROR_OTHER;
2670 }
2671
2672 num = persist_data->persist_valid_entries;
2673
2674 j = 0; // points to the end of non-deleted entries.
2675 // Filter out to-be-deleted entries in place.
2676 for (i = 0; i < num; i++) {
2677 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2678 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2679 j++;
2680 }
2681 }
2682
2683 if (j < num) {
2684 persist_data->persist_valid_entries = j;
2685 // Zeroise the remaining entries
2686 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2687 return PERSIST_DEL_KEY_OK;
2688 } else {
2689 // Did not find an entry matching the given fieldname
2690 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2691 }
2692}
2693
2694static int persist_count_keys(const char *fieldname)
2695{
2696 unsigned int i;
2697 unsigned int count;
2698
2699 if (persist_data == NULL) {
2700 return -1;
2701 }
2702
2703 count = 0;
2704 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2705 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2706 count++;
2707 }
2708 }
2709
2710 return count;
2711}
2712
Ken Sumrall160b4d62013-04-22 12:15:39 -07002713/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002714int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002715{
Paul Crowley38132a12016-02-09 09:50:32 +00002716 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002717 SLOGE("Cannot get field when file encrypted");
2718 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002719 }
2720
Ken Sumrall160b4d62013-04-22 12:15:39 -07002721 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002722 /* CRYPTO_GETFIELD_OK is success,
2723 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2724 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2725 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002726 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002727 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2728 int i;
2729 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002730
2731 if (persist_data == NULL) {
2732 load_persistent_data();
2733 if (persist_data == NULL) {
2734 SLOGE("Getfield error, cannot load persistent data");
2735 goto out;
2736 }
2737 }
2738
Rubin Xu85c01f92014-10-13 12:49:54 +01002739 // Read value from persistent entries. If the original value is split into multiple entries,
2740 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002741 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002742 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2743 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2744 // value too small
2745 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2746 goto out;
2747 }
2748 rc = CRYPTO_GETFIELD_OK;
2749
2750 for (i = 1; /* break explicitly */; i++) {
2751 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2752 (int) sizeof(temp_field)) {
2753 // If the fieldname is very long, we stop as soon as it begins to overflow the
2754 // maximum field length. At this point we have in fact fully read out the original
2755 // value because cryptfs_setfield would not allow fields with longer names to be
2756 // written in the first place.
2757 break;
2758 }
2759 if (!persist_get_key(temp_field, temp_value)) {
2760 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2761 // value too small.
2762 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2763 goto out;
2764 }
2765 } else {
2766 // Exhaust all entries.
2767 break;
2768 }
2769 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002770 } else {
2771 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002772 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002773 }
2774
2775out:
2776 return rc;
2777}
2778
2779/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002780int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002781{
Paul Crowley38132a12016-02-09 09:50:32 +00002782 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002783 SLOGE("Cannot set field when file encrypted");
2784 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002785 }
2786
Ken Sumrall160b4d62013-04-22 12:15:39 -07002787 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002788 /* 0 is success, negative values are error */
2789 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002790 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002791 unsigned int field_id;
2792 char temp_field[PROPERTY_KEY_MAX];
2793 unsigned int num_entries;
2794 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002795
2796 if (persist_data == NULL) {
2797 load_persistent_data();
2798 if (persist_data == NULL) {
2799 SLOGE("Setfield error, cannot load persistent data");
2800 goto out;
2801 }
2802 }
2803
2804 property_get("ro.crypto.state", encrypted_state, "");
2805 if (!strcmp(encrypted_state, "encrypted") ) {
2806 encrypted = 1;
2807 }
2808
Rubin Xu85c01f92014-10-13 12:49:54 +01002809 // Compute the number of entries required to store value, each entry can store up to
2810 // (PROPERTY_VALUE_MAX - 1) chars
2811 if (strlen(value) == 0) {
2812 // Empty value also needs one entry to store.
2813 num_entries = 1;
2814 } else {
2815 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2816 }
2817
2818 max_keylen = strlen(fieldname);
2819 if (num_entries > 1) {
2820 // Need an extra "_%d" suffix.
2821 max_keylen += 1 + log10(num_entries);
2822 }
2823 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2824 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002825 goto out;
2826 }
2827
Rubin Xu85c01f92014-10-13 12:49:54 +01002828 // Make sure we have enough space to write the new value
2829 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2830 persist_get_max_entries(encrypted)) {
2831 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2832 goto out;
2833 }
2834
2835 // Now that we know persist_data has enough space for value, let's delete the old field first
2836 // to make up space.
2837 persist_del_keys(fieldname, 0);
2838
2839 if (persist_set_key(fieldname, value, encrypted)) {
2840 // fail to set key, should not happen as we have already checked the available space
2841 SLOGE("persist_set_key() error during setfield()");
2842 goto out;
2843 }
2844
2845 for (field_id = 1; field_id < num_entries; field_id++) {
Greg Kaiserb610e772018-02-09 09:19:54 -08002846 snprintf(temp_field, sizeof(temp_field), "%s_%u", fieldname, field_id);
Rubin Xu85c01f92014-10-13 12:49:54 +01002847
2848 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2849 // fail to set key, should not happen as we have already checked the available space.
2850 SLOGE("persist_set_key() error during setfield()");
2851 goto out;
2852 }
2853 }
2854
Ken Sumrall160b4d62013-04-22 12:15:39 -07002855 /* If we are running encrypted, save the persistent data now */
2856 if (encrypted) {
2857 if (save_persistent_data()) {
2858 SLOGE("Setfield error, cannot save persistent data");
2859 goto out;
2860 }
2861 }
2862
Rubin Xu85c01f92014-10-13 12:49:54 +01002863 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002864
2865out:
2866 return rc;
2867}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002868
2869/* Checks userdata. Attempt to mount the volume if default-
2870 * encrypted.
2871 * On success trigger next init phase and return 0.
2872 * Currently do not handle failure - see TODO below.
2873 */
2874int cryptfs_mount_default_encrypted(void)
2875{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002876 int crypt_type = cryptfs_get_password_type();
2877 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2878 SLOGE("Bad crypt type - error");
2879 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2880 SLOGD("Password is not default - "
2881 "starting min framework to prompt");
2882 property_set("vold.decrypt", "trigger_restart_min_framework");
2883 return 0;
2884 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2885 SLOGD("Password is default - restarting filesystem");
2886 cryptfs_restart_internal(0);
2887 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002888 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002889 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002890 }
2891
Paul Lawrence6bfed202014-07-28 12:47:22 -07002892 /** Corrupt. Allow us to boot into framework, which will detect bad
2893 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002894 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002895 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002896 return 0;
2897}
2898
2899/* Returns type of the password, default, pattern, pin or password.
2900 */
2901int cryptfs_get_password_type(void)
2902{
Paul Crowley38132a12016-02-09 09:50:32 +00002903 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002904 SLOGE("cryptfs_get_password_type not valid for file encryption");
2905 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002906 }
2907
Paul Lawrencef4faa572014-01-29 13:31:03 -08002908 struct crypt_mnt_ftr crypt_ftr;
2909
2910 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2911 SLOGE("Error getting crypt footer and key\n");
2912 return -1;
2913 }
2914
Paul Lawrence6bfed202014-07-28 12:47:22 -07002915 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2916 return -1;
2917 }
2918
Paul Lawrencef4faa572014-01-29 13:31:03 -08002919 return crypt_ftr.crypt_type;
2920}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002921
Paul Lawrence05335c32015-03-05 09:46:23 -08002922const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002923{
Paul Crowley38132a12016-02-09 09:50:32 +00002924 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002925 SLOGE("cryptfs_get_password not valid for file encryption");
2926 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002927 }
2928
Paul Lawrence399317e2014-03-10 13:20:50 -07002929 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002930 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002931 if (now.tv_sec < password_expiry_time) {
2932 return password;
2933 } else {
2934 cryptfs_clear_password();
2935 return 0;
2936 }
2937}
2938
2939void cryptfs_clear_password()
2940{
2941 if (password) {
2942 size_t len = strlen(password);
2943 memset(password, 0, len);
2944 free(password);
2945 password = 0;
2946 password_expiry_time = 0;
2947 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002948}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002949
Paul Lawrence0c247462015-10-29 10:30:57 -07002950int cryptfs_isConvertibleToFBE()
2951{
Paul Crowleye2ee1522017-09-26 14:05:26 -07002952 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07002953 return (rec && fs_mgr_is_convertible_to_fbe(rec)) ? 1 : 0;
Paul Lawrence0c247462015-10-29 10:30:57 -07002954}