Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "MetadataCrypt.h" |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 18 | #include "KeyBuffer.h" |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 19 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <thread> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <fcntl.h> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 26 | #include <sys/param.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 32 | #include <android-base/properties.h> |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 33 | #include <android-base/unique_fd.h> |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 34 | #include <cutils/fs.h> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 35 | #include <fs_mgr.h> |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 36 | #include <libdm/dm.h> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 37 | |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 38 | #include "Checkpoint.h" |
Paul Crowley | 220567c | 2020-02-07 12:45:20 -0800 | [diff] [blame] | 39 | #include "CryptoType.h" |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 40 | #include "EncryptInplace.h" |
| 41 | #include "KeyStorage.h" |
| 42 | #include "KeyUtil.h" |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 43 | #include "Keymaster.h" |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 44 | #include "Utils.h" |
| 45 | #include "VoldUtil.h" |
| 46 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 47 | #define TABLE_LOAD_RETRIES 10 |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 48 | |
Paul Crowley | 220567c | 2020-02-07 12:45:20 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace vold { |
| 51 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 52 | using android::fs_mgr::FstabEntry; |
| 53 | using android::fs_mgr::GetEntryForMountPoint; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 54 | using android::vold::KeyBuffer; |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 55 | using namespace android::dm; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 56 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 57 | // Parsed from metadata options |
| 58 | struct CryptoOptions { |
| 59 | struct CryptoType cipher = invalid_crypto_type; |
| 60 | bool is_legacy = false; |
| 61 | bool set_dun = true; // Non-legacy driver always sets DUN |
| 62 | }; |
| 63 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 64 | static const std::string kDmNameUserdata = "userdata"; |
| 65 | |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 66 | static const char* kFn_keymaster_key_blob = "keymaster_key_blob"; |
| 67 | static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded"; |
| 68 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 69 | // The first entry in this table is the default crypto type. |
Paul Crowley | 220567c | 2020-02-07 12:45:20 -0800 | [diff] [blame] | 70 | constexpr CryptoType supported_crypto_types[] = {aes_256_xts, adiantum}; |
| 71 | |
| 72 | static_assert(validateSupportedCryptoTypes(64, supported_crypto_types, |
| 73 | array_length(supported_crypto_types)), |
| 74 | "We have a CryptoType which was incompletely constructed."); |
| 75 | |
| 76 | constexpr CryptoType legacy_aes_256_xts = |
| 77 | CryptoType().set_config_name("aes-256-xts").set_kernel_name("AES-256-XTS").set_keysize(64); |
| 78 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 79 | static_assert(isValidCryptoType(64, legacy_aes_256_xts), |
Paul Crowley | 220567c | 2020-02-07 12:45:20 -0800 | [diff] [blame] | 80 | "We have a CryptoType which was incompletely constructed."); |
| 81 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 82 | // Returns KeyGeneration suitable for key as described in CryptoOptions |
| 83 | const KeyGeneration makeGen(const CryptoOptions& options) { |
| 84 | return KeyGeneration{options.cipher.get_keysize(), true, false}; |
| 85 | } |
| 86 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 87 | static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) { |
Shawn Willden | 2b1ff5a | 2020-01-16 14:08:36 -0700 | [diff] [blame] | 88 | // We're about to mount data not verified by verified boot. Tell Keymaster that early boot has |
| 89 | // ended. |
| 90 | // |
| 91 | // TODO(paulcrowley): Make a Keymaster singleton or something, so we don't have to repeatedly |
| 92 | // open and initialize the service. |
| 93 | ::android::vold::Keymaster keymaster; |
| 94 | keymaster.earlyBootEnded(); |
| 95 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 96 | // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted |
| 97 | // partitions in the fsck domain. |
LongPing Wei | 7f3ab95 | 2019-01-30 16:03:14 +0800 | [diff] [blame] | 98 | if (setexeccon(android::vold::sFsckContext)) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 99 | PLOG(ERROR) << "Failed to setexeccon"; |
| 100 | return false; |
| 101 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 102 | auto mount_rc = fs_mgr_do_mount(&fstab_default, const_cast<char*>(mount_point), |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 103 | const_cast<char*>(blk_device), nullptr, |
| 104 | android::vold::cp_needsCheckpoint()); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 105 | if (setexeccon(nullptr)) { |
| 106 | PLOG(ERROR) << "Failed to clear setexeccon"; |
| 107 | return false; |
| 108 | } |
| 109 | if (mount_rc != 0) { |
| 110 | LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc; |
| 111 | return false; |
| 112 | } |
| 113 | LOG(DEBUG) << "Mounted " << mount_point; |
| 114 | return true; |
| 115 | } |
| 116 | |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 117 | // Note: It is possible to orphan a key if it is removed before deleting |
| 118 | // Update this once keymaster APIs change, and we have a proper commit. |
Greg Kaiser | 8ae16db | 2018-12-18 11:10:31 -0800 | [diff] [blame] | 119 | static void commit_key(const std::string& dir) { |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 120 | while (!android::base::WaitForProperty("vold.checkpoint_committed", "1")) { |
| 121 | LOG(ERROR) << "Wait for boot timed out"; |
| 122 | } |
| 123 | Keymaster keymaster; |
| 124 | auto keyPath = dir + "/" + kFn_keymaster_key_blob; |
| 125 | auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 126 | std::string key; |
| 127 | |
| 128 | if (!android::base::ReadFileToString(keyPath, &key)) { |
| 129 | LOG(ERROR) << "Failed to read old key: " << dir; |
| 130 | return; |
| 131 | } |
| 132 | if (rename(newKeyPath.c_str(), keyPath.c_str()) != 0) { |
| 133 | PLOG(ERROR) << "Unable to move upgraded key to location: " << keyPath; |
| 134 | return; |
| 135 | } |
| 136 | if (!keymaster.deleteKey(key)) { |
| 137 | LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir; |
| 138 | } |
| 139 | LOG(INFO) << "Old Key deleted: " << dir; |
| 140 | } |
| 141 | |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 142 | static bool read_key(const std::string& metadata_key_dir, const KeyGeneration& gen, |
| 143 | KeyBuffer* key) { |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 144 | if (metadata_key_dir.empty()) { |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 145 | LOG(ERROR) << "Failed to get metadata_key_dir"; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 148 | std::string sKey; |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 149 | auto dir = metadata_key_dir + "/key"; |
| 150 | LOG(DEBUG) << "metadata_key_dir/key: " << dir; |
Paul Crowley | 98a23a1 | 2018-05-09 13:01:16 -0700 | [diff] [blame] | 151 | if (fs_mkdirs(dir.c_str(), 0700)) { |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 152 | PLOG(ERROR) << "Creating directories: " << dir; |
Paul Crowley | 98a23a1 | 2018-05-09 13:01:16 -0700 | [diff] [blame] | 153 | return false; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 154 | } |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 155 | auto temp = metadata_key_dir + "/tmp"; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 156 | auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 157 | /* If we have a leftover upgraded key, delete it. |
| 158 | * We either failed an update and must return to the old key, |
| 159 | * or we rebooted before commiting the keys in a freak accident. |
| 160 | * Either way, we can re-upgrade the key if we need to. |
| 161 | */ |
| 162 | Keymaster keymaster; |
| 163 | if (pathExists(newKeyPath)) { |
| 164 | if (!android::base::ReadFileToString(newKeyPath, &sKey)) |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 165 | LOG(ERROR) << "Failed to read incomplete key: " << dir; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 166 | else if (!keymaster.deleteKey(sKey)) |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "Incomplete key deletion failed, continuing anyway: " << dir; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 168 | else |
| 169 | unlink(newKeyPath.c_str()); |
| 170 | } |
| 171 | bool needs_cp = cp_needsCheckpoint(); |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 172 | if (!retrieveOrGenerateKey(dir, temp, kEmptyAuthentication, gen, key, needs_cp)) return false; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 173 | if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach(); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 177 | static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) { |
Oleksiy Avramchenko | 625dc78 | 2018-05-23 10:50:46 +0200 | [diff] [blame] | 178 | if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 179 | PLOG(ERROR) << "Unable to measure size of " << real_blkdev; |
| 180 | return false; |
| 181 | } |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 185 | static bool create_crypto_blk_dev(const std::string& dm_name, const std::string& blk_device, |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 186 | const KeyBuffer& key, const CryptoOptions& options, |
| 187 | std::string* crypto_blkdev) { |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 188 | uint64_t nr_sec; |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 189 | if (!get_number_of_sectors(blk_device, &nr_sec)) return false; |
Paul Crowley | 84e84c5 | 2020-01-29 16:09:19 -0800 | [diff] [blame] | 190 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 191 | KeyBuffer hex_key_buffer; |
| 192 | if (android::vold::StrToHex(key, hex_key_buffer) != android::OK) { |
| 193 | LOG(ERROR) << "Failed to turn key to hex"; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 194 | return false; |
| 195 | } |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 196 | std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size()); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 197 | |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 198 | DmTable table; |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 199 | table.Emplace<DmTargetDefaultKey>(0, nr_sec, options.cipher.get_kernel_name(), hex_key, |
| 200 | blk_device, 0, options.is_legacy, options.set_dun); |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 201 | |
| 202 | auto& dm = DeviceMapper::Instance(); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 203 | for (int i = 0;; i++) { |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 204 | if (dm.CreateDevice(dm_name, table)) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 205 | break; |
| 206 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 207 | if (i + 1 >= TABLE_LOAD_RETRIES) { |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 208 | PLOG(ERROR) << "Could not create default-key device " << dm_name; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 211 | PLOG(INFO) << "Could not create default-key device, retrying"; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 212 | usleep(500000); |
| 213 | } |
| 214 | |
David Anderson | b922473 | 2019-05-13 13:02:54 -0700 | [diff] [blame] | 215 | if (!dm.GetDmDevicePathByName(dm_name, crypto_blkdev)) { |
| 216 | LOG(ERROR) << "Cannot retrieve default-key device status " << dm_name; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 222 | static const CryptoType& lookup_cipher(const std::string& cipher_name) { |
| 223 | if (cipher_name.empty()) return supported_crypto_types[0]; |
| 224 | for (size_t i = 0; i < array_length(supported_crypto_types); i++) { |
| 225 | if (cipher_name == supported_crypto_types[i].get_config_name()) { |
| 226 | return supported_crypto_types[i]; |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return invalid_crypto_type; |
| 230 | } |
| 231 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 232 | static bool parse_options(const std::string& options_string, CryptoOptions* options) { |
| 233 | options->cipher = lookup_cipher(options_string); |
| 234 | if (options->cipher.get_kernel_name() == nullptr) { |
| 235 | LOG(ERROR) << "No metadata cipher named " << options_string << " found"; |
| 236 | return false; |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 237 | } |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 238 | return true; |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 241 | bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point, |
| 242 | bool needs_encrypt) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 243 | LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 244 | auto encrypted_state = android::base::GetProperty("ro.crypto.state", ""); |
Nikita Ioffe | f850e6e | 2019-12-09 21:19:11 +0000 | [diff] [blame] | 245 | if (encrypted_state != "" && encrypted_state != "encrypted") { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 246 | LOG(DEBUG) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 247 | return false; |
| 248 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 249 | |
| 250 | auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 251 | if (!data_rec) { |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 252 | LOG(ERROR) << "Failed to get data_rec for " << mount_point; |
| 253 | return false; |
| 254 | } |
| 255 | if (blk_device != data_rec->blk_device) { |
| 256 | LOG(ERROR) << "blk_device " << blk_device << " does not match fstab entry " |
| 257 | << data_rec->blk_device << " for " << mount_point; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 260 | |
| 261 | bool is_legacy; |
| 262 | if (!DmTargetDefaultKey::IsLegacy(&is_legacy)) return false; |
| 263 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 264 | CryptoOptions options; |
| 265 | if (is_legacy) { |
| 266 | if (!data_rec->metadata_cipher.empty()) { |
| 267 | LOG(ERROR) << "metadata_cipher options cannot be set in legacy mode"; |
| 268 | return false; |
| 269 | } |
| 270 | options.cipher = legacy_aes_256_xts; |
| 271 | options.is_legacy = true; |
| 272 | options.set_dun = android::base::GetBoolProperty("ro.crypto.set_dun", false); |
| 273 | if (!options.set_dun && data_rec->fs_mgr_flags.checkpoint_blk) { |
| 274 | LOG(ERROR) |
| 275 | << "Block checkpoints and metadata encryption require ro.crypto.set_dun option"; |
| 276 | return false; |
| 277 | } |
| 278 | } else { |
| 279 | if (!parse_options(data_rec->metadata_cipher, &options)) return false; |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 282 | auto gen = needs_encrypt ? makeGen(options) : neverGen(); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 283 | KeyBuffer key; |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 284 | if (!read_key(data_rec->metadata_key_dir, gen, &key)) return false; |
Paul Lawrence | 4b140d3 | 2019-08-07 15:22:57 -0700 | [diff] [blame] | 285 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 286 | std::string crypto_blkdev; |
Paul Crowley | 249c2fb | 2020-02-07 12:51:56 -0800 | [diff] [blame^] | 287 | if (!create_crypto_blk_dev(kDmNameUserdata, data_rec->blk_device, key, options, &crypto_blkdev)) |
Paul Crowley | 572c024 | 2020-02-14 01:15:35 -0800 | [diff] [blame] | 288 | return false; |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 289 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 290 | // FIXME handle the corrupt case |
| 291 | if (needs_encrypt) { |
Paul Crowley | c9b92f0 | 2020-01-30 15:26:15 -0800 | [diff] [blame] | 292 | uint64_t nr_sec; |
| 293 | if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 294 | LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec; |
| 295 | off64_t size_already_done = 0; |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 296 | auto rc = cryptfs_enable_inplace(crypto_blkdev.data(), blk_device.data(), nr_sec, |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 297 | &size_already_done, nr_sec, 0, false); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 298 | if (rc != 0) { |
| 299 | LOG(ERROR) << "Inplace crypto failed with code: " << rc; |
| 300 | return false; |
| 301 | } |
| 302 | if (static_cast<uint64_t>(size_already_done) != nr_sec) { |
| 303 | LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done; |
| 304 | return false; |
| 305 | } |
| 306 | LOG(INFO) << "Inplace encryption complete"; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 307 | } |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 308 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 309 | LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point; |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 310 | mount_via_fs_mgr(data_rec->mount_point.c_str(), crypto_blkdev.c_str()); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 311 | return true; |
| 312 | } |
Paul Crowley | 220567c | 2020-02-07 12:45:20 -0800 | [diff] [blame] | 313 | |
| 314 | } // namespace vold |
| 315 | } // namespace android |