2016-06-02 20:04:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MetadataCrypt.h"
|
2018-09-18 22:30:21 +02:00
|
|
|
#include "KeyBuffer.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2018-01-30 18:48:19 +01:00
|
|
|
#include <android-base/properties.h>
|
2020-02-07 07:56:27 +01:00
|
|
|
#include <android-base/strings.h>
|
2017-06-16 18:21:18 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-01-30 18:48:19 +01:00
|
|
|
#include <cutils/fs.h>
|
2016-06-02 20:04:27 +02:00
|
|
|
#include <fs_mgr.h>
|
2019-05-13 22:02:54 +02:00
|
|
|
#include <libdm/dm.h>
|
2020-10-07 08:20:00 +02:00
|
|
|
#include <libgsi/libgsi.h>
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2018-08-28 10:58:49 +02:00
|
|
|
#include "Checkpoint.h"
|
2020-02-07 21:45:20 +01:00
|
|
|
#include "CryptoType.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
#include "EncryptInplace.h"
|
|
|
|
#include "KeyStorage.h"
|
|
|
|
#include "KeyUtil.h"
|
2021-06-15 20:34:00 +02:00
|
|
|
#include "Keystore.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "VoldUtil.h"
|
2020-12-15 18:00:49 +01:00
|
|
|
#include "fs/Ext4.h"
|
|
|
|
#include "fs/F2fs.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2020-02-07 21:45:20 +01:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2019-01-29 23:34:01 +01:00
|
|
|
using android::fs_mgr::FstabEntry;
|
|
|
|
using android::fs_mgr::GetEntryForMountPoint;
|
2020-04-06 04:34:31 +02:00
|
|
|
using android::fscrypt::GetFirstApiLevel;
|
2017-08-01 18:15:53 +02:00
|
|
|
using android::vold::KeyBuffer;
|
2019-05-13 22:02:54 +02:00
|
|
|
using namespace android::dm;
|
2021-09-22 02:21:57 +02:00
|
|
|
using namespace std::chrono_literals;
|
2017-08-01 18:15:53 +02:00
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
// Parsed from metadata options
|
|
|
|
struct CryptoOptions {
|
|
|
|
struct CryptoType cipher = invalid_crypto_type;
|
2020-03-22 16:02:06 +01:00
|
|
|
bool use_legacy_options_format = false;
|
2020-02-07 21:51:56 +01:00
|
|
|
bool set_dun = true; // Non-legacy driver always sets DUN
|
2020-02-07 07:56:27 +01:00
|
|
|
bool use_hw_wrapped_key = false;
|
2020-02-07 21:51:56 +01:00
|
|
|
};
|
|
|
|
|
2016-06-02 20:04:27 +02:00
|
|
|
static const std::string kDmNameUserdata = "userdata";
|
2020-12-15 18:02:29 +01:00
|
|
|
static const std::string kDmNameUserdataZoned = "userdata_zoned";
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
// The first entry in this table is the default crypto type.
|
2020-02-07 21:45:20 +01:00
|
|
|
constexpr CryptoType supported_crypto_types[] = {aes_256_xts, adiantum};
|
|
|
|
|
|
|
|
static_assert(validateSupportedCryptoTypes(64, supported_crypto_types,
|
|
|
|
array_length(supported_crypto_types)),
|
|
|
|
"We have a CryptoType which was incompletely constructed.");
|
|
|
|
|
|
|
|
constexpr CryptoType legacy_aes_256_xts =
|
|
|
|
CryptoType().set_config_name("aes-256-xts").set_kernel_name("AES-256-XTS").set_keysize(64);
|
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
static_assert(isValidCryptoType(64, legacy_aes_256_xts),
|
2020-02-07 21:45:20 +01:00
|
|
|
"We have a CryptoType which was incompletely constructed.");
|
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
// Returns KeyGeneration suitable for key as described in CryptoOptions
|
|
|
|
const KeyGeneration makeGen(const CryptoOptions& options) {
|
2020-02-07 07:56:27 +01:00
|
|
|
return KeyGeneration{options.cipher.get_keysize(), true, options.use_hw_wrapped_key};
|
2020-02-07 21:51:56 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:21:57 +02:00
|
|
|
void defaultkey_precreate_dm_device() {
|
|
|
|
auto& dm = DeviceMapper::Instance();
|
|
|
|
if (dm.GetState(kDmNameUserdata) != DmDeviceState::INVALID) {
|
|
|
|
LOG(INFO) << "Not pre-creating userdata encryption device; device already exists";
|
|
|
|
return;
|
|
|
|
}
|
2022-12-08 18:49:54 +01:00
|
|
|
|
2022-12-14 04:42:49 +01:00
|
|
|
if (!dm.CreatePlaceholderDevice(kDmNameUserdata)) {
|
2022-12-08 18:49:54 +01:00
|
|
|
LOG(ERROR) << "Failed to pre-create userdata metadata encryption device";
|
2021-09-22 02:21:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Do not reboot with commit failure when ext4 userdata is wiped
When userdata partition is the type of ext4, checkpoint and metadata
encryption are enabled, boot up the images, if the slot currently
in use is not marked as "successful" and userdata is wiped, a reboot
will be triggered because of "Checkpoint commit failed"
In this patch, do not create checkpoint when the preceding condition
occurs, otherwise "dm-default-key" sits below "dm-bow".
Because cp_needsCheckpoint(), cp_prepareCheckpoint() and cp_commitChanges()
are always executed when the system boots up, and now there is a condition
that BOW device is not created while the slot in use is not marked as
"successful", set "isBow" to be "false" if BOW state fails to be set to
1, if "isBow" is false, then there is no need to commit the checkpoint,
and the system won't automatically reboot.
Bug: 193457319
Test: i.MX 8M Plus EVK, images flashed to board, misc and userdata
partitions are wiped, checkpoint and metadata encryption are
enabled for ext4 userdata partition.
Signed-off-by: faqiang.zhu <faqiang.zhu@nxp.com>
Change-Id: I4ad47a9504f1be0407f0fd196001a0e96919be33
2021-07-19 06:27:37 +02:00
|
|
|
static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device, bool needs_encrypt) {
|
2016-06-02 20:04:27 +02:00
|
|
|
// fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
|
|
|
|
// partitions in the fsck domain.
|
2019-01-30 09:03:14 +01:00
|
|
|
if (setexeccon(android::vold::sFsckContext)) {
|
2016-06-02 20:04:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to setexeccon";
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-22 17:43:42 +02:00
|
|
|
auto mount_rc = fs_mgr_do_mount(&fstab_default, mount_point, blk_device,
|
|
|
|
android::vold::cp_needsCheckpoint(), needs_encrypt);
|
2016-06-02 20:04:27 +02:00
|
|
|
if (setexeccon(nullptr)) {
|
|
|
|
PLOG(ERROR) << "Failed to clear setexeccon";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mount_rc != 0) {
|
|
|
|
LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "Mounted " << mount_point;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-13 17:22:11 +01:00
|
|
|
static bool read_key(const std::string& metadata_key_dir, const KeyGeneration& gen, bool first_key,
|
2020-02-12 20:04:05 +01:00
|
|
|
KeyBuffer* key) {
|
2020-02-14 10:15:35 +01:00
|
|
|
if (metadata_key_dir.empty()) {
|
2020-01-31 00:26:15 +01:00
|
|
|
LOG(ERROR) << "Failed to get metadata_key_dir";
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-14 10:08:10 +01:00
|
|
|
std::string sKey;
|
2020-01-31 00:26:15 +01:00
|
|
|
auto dir = metadata_key_dir + "/key";
|
|
|
|
LOG(DEBUG) << "metadata_key_dir/key: " << dir;
|
2021-02-17 00:59:17 +01:00
|
|
|
if (!MkdirsSync(dir, 0700)) return false;
|
2021-09-14 08:40:59 +02:00
|
|
|
auto in_dsu = android::base::GetBoolProperty("ro.gsid.image_running", false);
|
|
|
|
// !pathExists(dir) does not imply there's a factory reset when in DSU mode.
|
2023-01-13 17:22:11 +01:00
|
|
|
if (!pathExists(dir) && !in_dsu && first_key) {
|
2021-08-07 00:16:10 +02:00
|
|
|
auto delete_all = android::base::GetBoolProperty(
|
|
|
|
"ro.crypto.metadata_init_delete_all_keys.enabled", false);
|
|
|
|
if (delete_all) {
|
|
|
|
LOG(INFO) << "Metadata key does not exist, calling deleteAllKeys";
|
|
|
|
Keystore::deleteAllKeys();
|
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "Metadata key does not exist but "
|
|
|
|
"ro.crypto.metadata_init_delete_all_keys.enabled is false";
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 00:26:15 +01:00
|
|
|
auto temp = metadata_key_dir + "/tmp";
|
2020-11-06 04:58:26 +01:00
|
|
|
return retrieveOrGenerateKey(dir, temp, kEmptyAuthentication, gen, key);
|
2016-06-02 20:04:27 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
|
2018-05-23 10:50:46 +02:00
|
|
|
if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
|
2016-06-02 20:04:27 +02:00
|
|
|
PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:15:35 +01:00
|
|
|
static bool create_crypto_blk_dev(const std::string& dm_name, const std::string& blk_device,
|
2020-02-07 21:51:56 +01:00
|
|
|
const KeyBuffer& key, const CryptoOptions& options,
|
2020-02-07 21:51:56 +01:00
|
|
|
std::string* crypto_blkdev, uint64_t* nr_sec) {
|
|
|
|
if (!get_number_of_sectors(blk_device, nr_sec)) return false;
|
|
|
|
// TODO(paulcrowley): don't hardcode that DmTargetDefaultKey uses 4096-byte
|
|
|
|
// sectors
|
|
|
|
*nr_sec &= ~7;
|
2020-01-30 01:09:19 +01:00
|
|
|
|
2020-02-07 07:56:27 +01:00
|
|
|
KeyBuffer module_key;
|
|
|
|
if (options.use_hw_wrapped_key) {
|
|
|
|
if (!exportWrappedStorageKey(key, &module_key)) {
|
|
|
|
LOG(ERROR) << "Failed to get ephemeral wrapped key";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
module_key = key;
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
KeyBuffer hex_key_buffer;
|
2020-02-07 07:56:27 +01:00
|
|
|
if (android::vold::StrToHex(module_key, hex_key_buffer) != android::OK) {
|
2019-05-13 22:02:54 +02:00
|
|
|
LOG(ERROR) << "Failed to turn key to hex";
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-13 22:02:54 +02:00
|
|
|
std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size());
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
auto target = std::make_unique<DmTargetDefaultKey>(0, *nr_sec, options.cipher.get_kernel_name(),
|
2020-02-07 07:56:27 +01:00
|
|
|
hex_key, blk_device, 0);
|
2020-03-22 16:02:06 +01:00
|
|
|
if (options.use_legacy_options_format) target->SetUseLegacyOptionsFormat();
|
2020-02-07 07:56:27 +01:00
|
|
|
if (options.set_dun) target->SetSetDun();
|
|
|
|
if (options.use_hw_wrapped_key) target->SetWrappedKeyV0();
|
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
DmTable table;
|
2020-02-07 07:56:27 +01:00
|
|
|
table.AddTarget(std::move(target));
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2020-01-31 00:26:15 +01:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
2021-09-22 02:21:57 +02:00
|
|
|
if (dm_name == kDmNameUserdata && dm.GetState(dm_name) == DmDeviceState::SUSPENDED) {
|
|
|
|
// The device was created in advance, populate it now.
|
|
|
|
if (!dm.LoadTableAndActivate(dm_name, table)) {
|
|
|
|
LOG(ERROR) << "Failed to populate default-key device " << dm_name;
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-06 11:43:52 +01:00
|
|
|
if (!dm.WaitForDevice(dm_name, 20s, crypto_blkdev)) {
|
2021-12-23 21:23:39 +01:00
|
|
|
LOG(ERROR) << "Failed to wait for default-key device " << dm_name;
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-22 02:21:57 +02:00
|
|
|
} else if (!dm.CreateDevice(dm_name, table, crypto_blkdev, 5s)) {
|
|
|
|
LOG(ERROR) << "Could not create default-key device " << dm_name;
|
2020-10-15 23:39:34 +02:00
|
|
|
return false;
|
2016-06-02 20:04:27 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
static const CryptoType& lookup_cipher(const std::string& cipher_name) {
|
|
|
|
if (cipher_name.empty()) return supported_crypto_types[0];
|
|
|
|
for (size_t i = 0; i < array_length(supported_crypto_types); i++) {
|
|
|
|
if (cipher_name == supported_crypto_types[i].get_config_name()) {
|
|
|
|
return supported_crypto_types[i];
|
2020-02-14 10:15:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return invalid_crypto_type;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
static bool parse_options(const std::string& options_string, CryptoOptions* options) {
|
2020-02-07 07:56:27 +01:00
|
|
|
auto parts = android::base::Split(options_string, ":");
|
|
|
|
if (parts.size() < 1 || parts.size() > 2) {
|
|
|
|
LOG(ERROR) << "Invalid metadata encryption option: " << options_string;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string cipher_name = parts[0];
|
|
|
|
options->cipher = lookup_cipher(cipher_name);
|
2020-02-07 21:51:56 +01:00
|
|
|
if (options->cipher.get_kernel_name() == nullptr) {
|
2020-02-07 07:56:27 +01:00
|
|
|
LOG(ERROR) << "No metadata cipher named " << cipher_name << " found";
|
2020-02-07 21:51:56 +01:00
|
|
|
return false;
|
2020-02-14 10:15:35 +01:00
|
|
|
}
|
2020-02-07 07:56:27 +01:00
|
|
|
|
|
|
|
if (parts.size() == 2) {
|
|
|
|
if (parts[1] == "wrappedkey_v0") {
|
|
|
|
options->use_hw_wrapped_key = true;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Invalid metadata encryption flag: " << parts[1];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 21:51:56 +01:00
|
|
|
return true;
|
2020-02-14 10:15:35 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 23:44:33 +02:00
|
|
|
bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point,
|
2020-12-15 18:00:49 +01:00
|
|
|
bool needs_encrypt, bool should_format,
|
2020-12-15 18:02:29 +01:00
|
|
|
const std::string& fs_type, const std::string& zoned_device) {
|
2020-12-15 18:00:49 +01:00
|
|
|
LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point
|
|
|
|
<< " encrypt: " << needs_encrypt << " format: " << should_format << " with "
|
2020-12-15 18:02:29 +01:00
|
|
|
<< fs_type << " block device: " << blk_device
|
|
|
|
<< " and zoned device: " << zoned_device;
|
2018-01-30 18:48:19 +01:00
|
|
|
auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
|
2019-12-09 22:19:11 +01:00
|
|
|
if (encrypted_state != "" && encrypted_state != "encrypted") {
|
2021-11-06 02:57:49 +01:00
|
|
|
LOG(ERROR) << "fscrypt_mount_metadata_encrypted got unexpected starting state: "
|
|
|
|
<< encrypted_state;
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
|
|
|
|
auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point);
|
2016-06-02 20:04:27 +02:00
|
|
|
if (!data_rec) {
|
2020-01-31 00:26:15 +01:00
|
|
|
LOG(ERROR) << "Failed to get data_rec for " << mount_point;
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-14 10:15:35 +01:00
|
|
|
|
2020-03-22 16:02:06 +01:00
|
|
|
unsigned int options_format_version = android::base::GetUintProperty<unsigned int>(
|
|
|
|
"ro.crypto.dm_default_key.options_format.version",
|
2020-08-10 19:55:56 +02:00
|
|
|
(GetFirstApiLevel() <= __ANDROID_API_Q__ ? 1 : 2));
|
2020-02-14 10:15:35 +01:00
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
CryptoOptions options;
|
2020-03-22 16:02:06 +01:00
|
|
|
if (options_format_version == 1) {
|
2022-03-18 00:18:18 +01:00
|
|
|
if (!data_rec->metadata_encryption_options.empty()) {
|
2020-02-07 07:56:27 +01:00
|
|
|
LOG(ERROR) << "metadata_encryption options cannot be set in legacy mode";
|
2020-02-07 21:51:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
options.cipher = legacy_aes_256_xts;
|
2020-03-22 16:02:06 +01:00
|
|
|
options.use_legacy_options_format = true;
|
2020-02-07 21:51:56 +01:00
|
|
|
options.set_dun = android::base::GetBoolProperty("ro.crypto.set_dun", false);
|
|
|
|
if (!options.set_dun && data_rec->fs_mgr_flags.checkpoint_blk) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "Block checkpoints and metadata encryption require ro.crypto.set_dun option";
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-22 16:02:06 +01:00
|
|
|
} else if (options_format_version == 2) {
|
2022-03-18 00:18:18 +01:00
|
|
|
if (!parse_options(data_rec->metadata_encryption_options, &options)) return false;
|
2020-03-22 16:02:06 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unknown options_format_version: " << options_format_version;
|
|
|
|
return false;
|
2020-02-14 10:15:35 +01:00
|
|
|
}
|
|
|
|
|
2020-12-15 18:02:29 +01:00
|
|
|
auto default_metadata_key_dir = data_rec->metadata_key_dir;
|
|
|
|
if (!zoned_device.empty()) {
|
|
|
|
default_metadata_key_dir = default_metadata_key_dir + "/default";
|
|
|
|
}
|
2020-02-07 21:51:56 +01:00
|
|
|
auto gen = needs_encrypt ? makeGen(options) : neverGen();
|
2020-01-31 00:26:15 +01:00
|
|
|
KeyBuffer key;
|
2023-01-13 17:22:11 +01:00
|
|
|
if (!read_key(default_metadata_key_dir, gen, true, &key)) {
|
2021-11-06 02:57:49 +01:00
|
|
|
LOG(ERROR) << "read_key failed in mountFstab";
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-08 00:22:57 +02:00
|
|
|
|
2016-06-02 20:04:27 +02:00
|
|
|
std::string crypto_blkdev;
|
2020-02-07 21:51:56 +01:00
|
|
|
uint64_t nr_sec;
|
2021-11-06 02:57:49 +01:00
|
|
|
if (!create_crypto_blk_dev(kDmNameUserdata, blk_device, key, options, &crypto_blkdev,
|
|
|
|
&nr_sec)) {
|
|
|
|
LOG(ERROR) << "create_crypto_blk_dev failed in mountFstab";
|
2020-02-14 10:15:35 +01:00
|
|
|
return false;
|
2021-11-06 02:57:49 +01:00
|
|
|
}
|
2019-06-25 23:44:33 +02:00
|
|
|
|
2020-12-15 18:02:29 +01:00
|
|
|
// create dm-default-key for zoned device
|
|
|
|
std::string crypto_zoned_blkdev;
|
|
|
|
if (!zoned_device.empty()) {
|
|
|
|
auto zoned_metadata_key_dir = data_rec->metadata_key_dir + "/zoned";
|
|
|
|
|
2023-01-13 17:22:11 +01:00
|
|
|
if (!read_key(zoned_metadata_key_dir, gen, false, &key)) {
|
2020-12-15 18:02:29 +01:00
|
|
|
LOG(ERROR) << "read_key failed with zoned device: " << zoned_device;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!create_crypto_blk_dev(kDmNameUserdataZoned, zoned_device, key, options,
|
|
|
|
&crypto_zoned_blkdev, &nr_sec)) {
|
|
|
|
LOG(ERROR) << "fscrypt_mount_metadata_encrypted: failed with zoned device: "
|
|
|
|
<< zoned_device;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 18:00:49 +01:00
|
|
|
if (needs_encrypt) {
|
|
|
|
if (should_format) {
|
|
|
|
status_t error;
|
|
|
|
|
|
|
|
if (fs_type == "ext4") {
|
|
|
|
error = ext4::Format(crypto_blkdev, 0, mount_point);
|
|
|
|
} else if (fs_type == "f2fs") {
|
2020-12-15 18:02:29 +01:00
|
|
|
error = f2fs::Format(crypto_blkdev, crypto_zoned_blkdev);
|
2020-12-15 18:00:49 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unknown filesystem type: " << fs_type;
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 02:57:49 +01:00
|
|
|
if (error != 0) {
|
|
|
|
LOG(ERROR) << "Format of " << crypto_blkdev << " for " << mount_point
|
|
|
|
<< " failed (err=" << error << ").";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "Format of " << crypto_blkdev << " for " << mount_point << " succeeded.";
|
2020-12-15 18:00:49 +01:00
|
|
|
} else {
|
2020-12-15 18:02:29 +01:00
|
|
|
if (!zoned_device.empty()) {
|
|
|
|
LOG(ERROR) << "encrypt_inplace cannot support zoned device; should format it.";
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-09 21:39:04 +01:00
|
|
|
if (!encrypt_inplace(crypto_blkdev, blk_device, nr_sec)) {
|
2021-11-06 02:57:49 +01:00
|
|
|
LOG(ERROR) << "encrypt_inplace failed in mountFstab";
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-15 18:00:49 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2018-01-30 18:48:19 +01:00
|
|
|
LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
|
Do not reboot with commit failure when ext4 userdata is wiped
When userdata partition is the type of ext4, checkpoint and metadata
encryption are enabled, boot up the images, if the slot currently
in use is not marked as "successful" and userdata is wiped, a reboot
will be triggered because of "Checkpoint commit failed"
In this patch, do not create checkpoint when the preceding condition
occurs, otherwise "dm-default-key" sits below "dm-bow".
Because cp_needsCheckpoint(), cp_prepareCheckpoint() and cp_commitChanges()
are always executed when the system boots up, and now there is a condition
that BOW device is not created while the slot in use is not marked as
"successful", set "isBow" to be "false" if BOW state fails to be set to
1, if "isBow" is false, then there is no need to commit the checkpoint,
and the system won't automatically reboot.
Bug: 193457319
Test: i.MX 8M Plus EVK, images flashed to board, misc and userdata
partitions are wiped, checkpoint and metadata encryption are
enabled for ext4 userdata partition.
Signed-off-by: faqiang.zhu <faqiang.zhu@nxp.com>
Change-Id: I4ad47a9504f1be0407f0fd196001a0e96919be33
2021-07-19 06:27:37 +02:00
|
|
|
mount_via_fs_mgr(mount_point.c_str(), crypto_blkdev.c_str(), needs_encrypt);
|
2020-03-23 16:59:12 +01:00
|
|
|
|
|
|
|
// Record that there's at least one fstab entry with metadata encryption
|
|
|
|
if (!android::base::SetProperty("ro.crypto.metadata.enabled", "true")) {
|
|
|
|
LOG(WARNING) << "failed to set ro.crypto.metadata.enabled"; // This isn't fatal
|
|
|
|
}
|
2016-06-02 20:04:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-02-07 21:45:20 +01:00
|
|
|
|
2020-02-07 21:51:56 +01:00
|
|
|
static bool get_volume_options(CryptoOptions* options) {
|
|
|
|
return parse_options(android::base::GetProperty("ro.crypto.volume.metadata.encryption", ""),
|
|
|
|
options);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool defaultkey_volume_keygen(KeyGeneration* gen) {
|
|
|
|
CryptoOptions options;
|
|
|
|
if (!get_volume_options(&options)) return false;
|
|
|
|
*gen = makeGen(options);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool defaultkey_setup_ext_volume(const std::string& label, const std::string& blk_device,
|
|
|
|
const KeyBuffer& key, std::string* out_crypto_blkdev) {
|
|
|
|
LOG(DEBUG) << "defaultkey_setup_ext_volume: " << label << " " << blk_device;
|
|
|
|
|
|
|
|
CryptoOptions options;
|
|
|
|
if (!get_volume_options(&options)) return false;
|
|
|
|
uint64_t nr_sec;
|
|
|
|
return create_crypto_blk_dev(label, blk_device, key, options, out_crypto_blkdev, &nr_sec);
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:20:00 +02:00
|
|
|
bool destroy_dsu_metadata_key(const std::string& dsu_slot) {
|
|
|
|
LOG(DEBUG) << "destroy_dsu_metadata_key: " << dsu_slot;
|
|
|
|
|
|
|
|
const auto dsu_metadata_key_dir = android::gsi::GetDsuMetadataKeyDir(dsu_slot);
|
|
|
|
if (!pathExists(dsu_metadata_key_dir)) {
|
|
|
|
LOG(DEBUG) << "DSU metadata_key_dir doesn't exist, nothing to remove: "
|
|
|
|
<< dsu_metadata_key_dir;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the DSU key directory is different from the host OS'.
|
|
|
|
// Under normal circumstances, this should never happen, but handle it just in case.
|
|
|
|
if (auto data_rec = GetEntryForMountPoint(&fstab_default, "/data")) {
|
|
|
|
if (dsu_metadata_key_dir == data_rec->metadata_key_dir) {
|
|
|
|
LOG(ERROR) << "DSU metadata_key_dir is same as host OS: " << dsu_metadata_key_dir;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
for (auto suffix : {"/key", "/tmp"}) {
|
|
|
|
const auto key_path = dsu_metadata_key_dir + suffix;
|
|
|
|
if (pathExists(key_path)) {
|
|
|
|
LOG(DEBUG) << "Destroy key: " << key_path;
|
|
|
|
if (!android::vold::destroyKey(key_path)) {
|
|
|
|
LOG(ERROR) << "Failed to destroyKey(): " << key_path;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Remove DSU metadata_key_dir: " << dsu_metadata_key_dir;
|
|
|
|
// DeleteDirContentsAndDir() already logged any error, so don't log repeatedly.
|
|
|
|
return android::vold::DeleteDirContentsAndDir(dsu_metadata_key_dir) == android::OK;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:45:20 +01:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|