blob: 8d835410f1111e4c4e9dbe4099006b0d12b7620e [file] [log] [blame]
Paul Crowleyd5759812016-06-02 11:04:27 -07001/*
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 Crowley14c8c072018-09-18 13:30:21 -070018#include "KeyBuffer.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070019
Daniel Rosenberg358487a2024-07-29 13:48:38 -070020#include <fstream>
Paul Crowleyd5759812016-06-02 11:04:27 -070021#include <string>
Paul Crowleyd5759812016-06-02 11:04:27 -070022
23#include <fcntl.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070024#include <sys/param.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Paul Crowleyd5759812016-06-02 11:04:27 -070028#include <android-base/logging.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080029#include <android-base/properties.h>
Barani Muthukumaran312b7df2020-02-06 22:56:27 -080030#include <android-base/strings.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070031#include <android-base/unique_fd.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080032#include <cutils/fs.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070033#include <fs_mgr.h>
David Andersonb9224732019-05-13 13:02:54 -070034#include <libdm/dm.h>
Yo Chiang0af25a32020-10-07 14:20:00 +080035#include <libgsi/libgsi.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070036
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070037#include "Checkpoint.h"
Paul Crowley220567c2020-02-07 12:45:20 -080038#include "CryptoType.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070039#include "EncryptInplace.h"
40#include "KeyStorage.h"
41#include "KeyUtil.h"
Eric Biggersd86a8ab2021-06-15 11:34:00 -070042#include "Keystore.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070043#include "Utils.h"
44#include "VoldUtil.h"
Jaegeuk Kim0c52c712020-12-15 09:00:49 -080045#include "fs/Ext4.h"
46#include "fs/F2fs.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070047
Paul Crowley220567c2020-02-07 12:45:20 -080048namespace android {
49namespace vold {
50
Jaegeuk Kim2091c872024-04-23 19:01:26 -070051using android::base::Basename;
Tom Cherry4c5bde22019-01-29 14:34:01 -080052using android::fs_mgr::FstabEntry;
53using android::fs_mgr::GetEntryForMountPoint;
Paul Crowleyf4430382020-04-05 19:34:31 -070054using android::fscrypt::GetFirstApiLevel;
Pavel Grafove2e2d302017-08-01 17:15:53 +010055using android::vold::KeyBuffer;
David Andersonb9224732019-05-13 13:02:54 -070056using namespace android::dm;
David Anderson156d9d22021-09-21 17:21:57 -070057using namespace std::chrono_literals;
Pavel Grafove2e2d302017-08-01 17:15:53 +010058
Paul Crowley249c2fb2020-02-07 12:51:56 -080059// Parsed from metadata options
60struct CryptoOptions {
61 struct CryptoType cipher = invalid_crypto_type;
Paul Crowleyf56d5532020-03-22 08:02:06 -070062 bool use_legacy_options_format = false;
Paul Crowley249c2fb2020-02-07 12:51:56 -080063 bool set_dun = true; // Non-legacy driver always sets DUN
Barani Muthukumaran312b7df2020-02-06 22:56:27 -080064 bool use_hw_wrapped_key = false;
Paul Crowley249c2fb2020-02-07 12:51:56 -080065};
66
Paul Crowleyd5759812016-06-02 11:04:27 -070067static const std::string kDmNameUserdata = "userdata";
68
Paul Crowley249c2fb2020-02-07 12:51:56 -080069// The first entry in this table is the default crypto type.
Paul Crowley220567c2020-02-07 12:45:20 -080070constexpr CryptoType supported_crypto_types[] = {aes_256_xts, adiantum};
71
72static_assert(validateSupportedCryptoTypes(64, supported_crypto_types,
73 array_length(supported_crypto_types)),
74 "We have a CryptoType which was incompletely constructed.");
75
76constexpr CryptoType legacy_aes_256_xts =
77 CryptoType().set_config_name("aes-256-xts").set_kernel_name("AES-256-XTS").set_keysize(64);
78
Paul Crowley249c2fb2020-02-07 12:51:56 -080079static_assert(isValidCryptoType(64, legacy_aes_256_xts),
Paul Crowley220567c2020-02-07 12:45:20 -080080 "We have a CryptoType which was incompletely constructed.");
81
Paul Crowley249c2fb2020-02-07 12:51:56 -080082// Returns KeyGeneration suitable for key as described in CryptoOptions
83const KeyGeneration makeGen(const CryptoOptions& options) {
Barani Muthukumaran312b7df2020-02-06 22:56:27 -080084 return KeyGeneration{options.cipher.get_keysize(), true, options.use_hw_wrapped_key};
Paul Crowley249c2fb2020-02-07 12:51:56 -080085}
86
David Anderson156d9d22021-09-21 17:21:57 -070087void defaultkey_precreate_dm_device() {
88 auto& dm = DeviceMapper::Instance();
89 if (dm.GetState(kDmNameUserdata) != DmDeviceState::INVALID) {
90 LOG(INFO) << "Not pre-creating userdata encryption device; device already exists";
91 return;
92 }
David Anderson7b769bc2022-12-08 09:49:54 -080093
David Anderson223c1b22022-12-13 19:42:49 -080094 if (!dm.CreatePlaceholderDevice(kDmNameUserdata)) {
David Anderson7b769bc2022-12-08 09:49:54 -080095 LOG(ERROR) << "Failed to pre-create userdata metadata encryption device";
96 }
David Anderson156d9d22021-09-21 17:21:57 -070097}
98
faqiang.zhudd20dc32021-07-19 12:27:37 +080099static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device, bool needs_encrypt) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700100 // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
101 // partitions in the fsck domain.
LongPing Wei7f3ab952019-01-30 16:03:14 +0800102 if (setexeccon(android::vold::sFsckContext)) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700103 PLOG(ERROR) << "Failed to setexeccon";
104 return false;
105 }
Paul Lawrence997e9bb2023-06-22 08:43:42 -0700106 auto mount_rc = fs_mgr_do_mount(&fstab_default, mount_point, blk_device,
107 android::vold::cp_needsCheckpoint(), needs_encrypt);
Paul Crowleyd5759812016-06-02 11:04:27 -0700108 if (setexeccon(nullptr)) {
109 PLOG(ERROR) << "Failed to clear setexeccon";
110 return false;
111 }
112 if (mount_rc != 0) {
113 LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc;
114 return false;
115 }
116 LOG(DEBUG) << "Mounted " << mount_point;
117 return true;
118}
119
Jaegeuk Kimfb9aada2023-01-13 08:22:11 -0800120static bool read_key(const std::string& metadata_key_dir, const KeyGeneration& gen, bool first_key,
Paul Crowley4eac2642020-02-12 11:04:05 -0800121 KeyBuffer* key) {
Paul Crowley572c0242020-02-14 01:15:35 -0800122 if (metadata_key_dir.empty()) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800123 LOG(ERROR) << "Failed to get metadata_key_dir";
Paul Crowleyd5759812016-06-02 11:04:27 -0700124 return false;
125 }
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800126 std::string sKey;
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800127 auto dir = metadata_key_dir + "/key";
128 LOG(DEBUG) << "metadata_key_dir/key: " << dir;
Eric Biggersfec0c0e2021-02-16 15:59:17 -0800129 if (!MkdirsSync(dir, 0700)) return false;
Howard Chencbc1bdb2021-09-14 14:40:59 +0800130 auto in_dsu = android::base::GetBoolProperty("ro.gsid.image_running", false);
131 // !pathExists(dir) does not imply there's a factory reset when in DSU mode.
Jaegeuk Kimfb9aada2023-01-13 08:22:11 -0800132 if (!pathExists(dir) && !in_dsu && first_key) {
Paul Crowley1e6a5f52021-08-06 15:16:10 -0700133 auto delete_all = android::base::GetBoolProperty(
134 "ro.crypto.metadata_init_delete_all_keys.enabled", false);
135 if (delete_all) {
136 LOG(INFO) << "Metadata key does not exist, calling deleteAllKeys";
137 Keystore::deleteAllKeys();
138 } else {
139 LOG(DEBUG) << "Metadata key does not exist but "
140 "ro.crypto.metadata_init_delete_all_keys.enabled is false";
141 }
142 }
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800143 auto temp = metadata_key_dir + "/tmp";
Eric Biggersf74373b2020-11-05 19:58:26 -0800144 return retrieveOrGenerateKey(dir, temp, kEmptyAuthentication, gen, key);
Paul Crowleyd5759812016-06-02 11:04:27 -0700145}
146
Paul Crowley14c8c072018-09-18 13:30:21 -0700147static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200148 if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700149 PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
150 return false;
151 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700152 return true;
153}
154
Paul Crowley572c0242020-02-14 01:15:35 -0800155static bool create_crypto_blk_dev(const std::string& dm_name, const std::string& blk_device,
Paul Crowley249c2fb2020-02-07 12:51:56 -0800156 const KeyBuffer& key, const CryptoOptions& options,
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700157 std::string* crypto_blkdev, uint64_t* nr_sec, bool is_userdata) {
Paul Crowley886e5722020-02-07 12:51:56 -0800158 if (!get_number_of_sectors(blk_device, nr_sec)) return false;
159 // TODO(paulcrowley): don't hardcode that DmTargetDefaultKey uses 4096-byte
160 // sectors
161 *nr_sec &= ~7;
Paul Crowley84e84c52020-01-29 16:09:19 -0800162
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800163 KeyBuffer module_key;
164 if (options.use_hw_wrapped_key) {
165 if (!exportWrappedStorageKey(key, &module_key)) {
166 LOG(ERROR) << "Failed to get ephemeral wrapped key";
167 return false;
168 }
169 } else {
170 module_key = key;
171 }
172
David Andersonb9224732019-05-13 13:02:54 -0700173 KeyBuffer hex_key_buffer;
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800174 if (android::vold::StrToHex(module_key, hex_key_buffer) != android::OK) {
David Andersonb9224732019-05-13 13:02:54 -0700175 LOG(ERROR) << "Failed to turn key to hex";
Paul Crowleyd5759812016-06-02 11:04:27 -0700176 return false;
177 }
David Andersonb9224732019-05-13 13:02:54 -0700178 std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size());
Paul Crowleyd5759812016-06-02 11:04:27 -0700179
Paul Crowley886e5722020-02-07 12:51:56 -0800180 auto target = std::make_unique<DmTargetDefaultKey>(0, *nr_sec, options.cipher.get_kernel_name(),
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800181 hex_key, blk_device, 0);
Paul Crowleyf56d5532020-03-22 08:02:06 -0700182 if (options.use_legacy_options_format) target->SetUseLegacyOptionsFormat();
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800183 if (options.set_dun) target->SetSetDun();
184 if (options.use_hw_wrapped_key) target->SetWrappedKeyV0();
185
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800186 DmTable table;
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800187 table.AddTarget(std::move(target));
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800188
189 auto& dm = DeviceMapper::Instance();
David Anderson156d9d22021-09-21 17:21:57 -0700190 if (dm_name == kDmNameUserdata && dm.GetState(dm_name) == DmDeviceState::SUSPENDED) {
191 // The device was created in advance, populate it now.
David Anderson156d9d22021-09-21 17:21:57 -0700192 if (!dm.LoadTableAndActivate(dm_name, table)) {
193 LOG(ERROR) << "Failed to populate default-key device " << dm_name;
194 return false;
195 }
Masaya Takahashi5ed64b22023-02-06 19:43:52 +0900196 if (!dm.WaitForDevice(dm_name, 20s, crypto_blkdev)) {
Will McVickerb910e7e2021-12-23 12:23:39 -0800197 LOG(ERROR) << "Failed to wait for default-key device " << dm_name;
198 return false;
199 }
David Anderson156d9d22021-09-21 17:21:57 -0700200 } else if (!dm.CreateDevice(dm_name, table, crypto_blkdev, 5s)) {
201 LOG(ERROR) << "Could not create default-key device " << dm_name;
Eric Biggers836b51b2020-10-15 14:39:34 -0700202 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700203 }
Jaegeuk Kim95c61b32023-11-10 15:16:14 -0800204
205 // If there are multiple partitions used for a single mount, F2FS stores
206 // their partition paths in superblock. If the paths are dm targets, we
207 // cannot guarantee them across device boots. Let's use the logical paths.
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700208 if (is_userdata) {
Jaegeuk Kim95c61b32023-11-10 15:16:14 -0800209 *crypto_blkdev = "/dev/block/mapper/" + dm_name;
210 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700211 return true;
212}
213
Paul Crowley249c2fb2020-02-07 12:51:56 -0800214static const CryptoType& lookup_cipher(const std::string& cipher_name) {
215 if (cipher_name.empty()) return supported_crypto_types[0];
216 for (size_t i = 0; i < array_length(supported_crypto_types); i++) {
217 if (cipher_name == supported_crypto_types[i].get_config_name()) {
218 return supported_crypto_types[i];
Paul Crowley572c0242020-02-14 01:15:35 -0800219 }
220 }
221 return invalid_crypto_type;
222}
223
Paul Crowley249c2fb2020-02-07 12:51:56 -0800224static bool parse_options(const std::string& options_string, CryptoOptions* options) {
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800225 auto parts = android::base::Split(options_string, ":");
226 if (parts.size() < 1 || parts.size() > 2) {
227 LOG(ERROR) << "Invalid metadata encryption option: " << options_string;
Paul Crowley249c2fb2020-02-07 12:51:56 -0800228 return false;
Paul Crowley572c0242020-02-14 01:15:35 -0800229 }
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800230 std::string cipher_name = parts[0];
231 options->cipher = lookup_cipher(cipher_name);
232 if (options->cipher.get_kernel_name() == nullptr) {
233 LOG(ERROR) << "No metadata cipher named " << cipher_name << " found";
234 return false;
235 }
236
237 if (parts.size() == 2) {
238 if (parts[1] == "wrappedkey_v0") {
239 options->use_hw_wrapped_key = true;
240 } else {
241 LOG(ERROR) << "Invalid metadata encryption flag: " << parts[1];
242 return false;
243 }
244 }
Paul Crowley249c2fb2020-02-07 12:51:56 -0800245 return true;
Paul Crowley572c0242020-02-14 01:15:35 -0800246}
247
Daniel Rosenberg358487a2024-07-29 13:48:38 -0700248class EncryptionInProgress {
249 private:
250 std::string file_path_;
251 bool need_cleanup_ = false;
252
253 public:
254 EncryptionInProgress(const FstabEntry& entry) {
255 file_path_ = fs_mgr_metadata_encryption_in_progress_file_name(entry);
256 }
257
258 [[nodiscard]] bool Mark() {
259 {
260 std::ofstream touch(file_path_);
261 if (!touch.is_open()) {
262 PLOG(ERROR) << "Failed to mark metadata encryption in progress " << file_path_;
263 return false;
264 }
265 need_cleanup_ = true;
266 }
267 if (!android::vold::FsyncParentDirectory(file_path_)) return false;
268
269 LOG(INFO) << "Marked metadata encryption in progress (" << file_path_ << ")";
270 return true;
271 }
272
273 [[nodiscard]] bool Remove() {
274 need_cleanup_ = false;
275 if (unlink(file_path_.c_str()) != 0) {
276 PLOG(ERROR) << "Failed to clear metadata encryption in progress (" << file_path_ << ")";
277 return false;
278 }
279 if (!android::vold::FsyncParentDirectory(file_path_)) return false;
280
281 LOG(INFO) << "Cleared metadata encryption in progress (" << file_path_ << ")";
282 return true;
283 }
284
285 ~EncryptionInProgress() {
286 if (need_cleanup_) (void)Remove();
287 }
288};
289
Paul Lawrence236e5e82019-06-25 14:44:33 -0700290bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point,
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800291 bool needs_encrypt, bool should_format,
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700292 const std::string& fs_type, bool is_zoned,
Ashok Mutyala8a398782024-05-28 09:31:44 +0000293 const std::vector<std::string>& user_devices,
294 int64_t length) {
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800295 LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point
296 << " encrypt: " << needs_encrypt << " format: " << should_format << " with "
Ashok Mutyala8a398782024-05-28 09:31:44 +0000297 << fs_type << " block device: " << blk_device << " with zoned " << is_zoned
298 << " length: " << length;
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700299
300 for (auto& device : user_devices) {
301 LOG(DEBUG) << " - user devices: " << device;
302 }
303
Paul Crowley0fd26262018-01-30 09:48:19 -0800304 auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
Nikita Ioffef850e6e2019-12-09 21:19:11 +0000305 if (encrypted_state != "" && encrypted_state != "encrypted") {
David Andersone1791572021-11-05 18:57:49 -0700306 LOG(ERROR) << "fscrypt_mount_metadata_encrypted got unexpected starting state: "
307 << encrypted_state;
Paul Crowleyd5759812016-06-02 11:04:27 -0700308 return false;
309 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800310
311 auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point);
Paul Crowleyd5759812016-06-02 11:04:27 -0700312 if (!data_rec) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800313 LOG(ERROR) << "Failed to get data_rec for " << mount_point;
314 return false;
315 }
Paul Crowley572c0242020-02-14 01:15:35 -0800316
Paul Crowleyf56d5532020-03-22 08:02:06 -0700317 unsigned int options_format_version = android::base::GetUintProperty<unsigned int>(
318 "ro.crypto.dm_default_key.options_format.version",
Eric Biggers72d07132020-08-10 10:55:56 -0700319 (GetFirstApiLevel() <= __ANDROID_API_Q__ ? 1 : 2));
Paul Crowley572c0242020-02-14 01:15:35 -0800320
Paul Crowley249c2fb2020-02-07 12:51:56 -0800321 CryptoOptions options;
Paul Crowleyf56d5532020-03-22 08:02:06 -0700322 if (options_format_version == 1) {
Eric Biggers41d78432022-03-17 23:18:18 +0000323 if (!data_rec->metadata_encryption_options.empty()) {
Barani Muthukumaran312b7df2020-02-06 22:56:27 -0800324 LOG(ERROR) << "metadata_encryption options cannot be set in legacy mode";
Paul Crowley249c2fb2020-02-07 12:51:56 -0800325 return false;
326 }
327 options.cipher = legacy_aes_256_xts;
Paul Crowleyf56d5532020-03-22 08:02:06 -0700328 options.use_legacy_options_format = true;
Paul Crowley249c2fb2020-02-07 12:51:56 -0800329 options.set_dun = android::base::GetBoolProperty("ro.crypto.set_dun", false);
330 if (!options.set_dun && data_rec->fs_mgr_flags.checkpoint_blk) {
331 LOG(ERROR)
332 << "Block checkpoints and metadata encryption require ro.crypto.set_dun option";
333 return false;
334 }
Paul Crowleyf56d5532020-03-22 08:02:06 -0700335 } else if (options_format_version == 2) {
Eric Biggers41d78432022-03-17 23:18:18 +0000336 if (!parse_options(data_rec->metadata_encryption_options, &options)) return false;
Paul Crowleyf56d5532020-03-22 08:02:06 -0700337 } else {
338 LOG(ERROR) << "Unknown options_format_version: " << options_format_version;
339 return false;
Paul Crowley572c0242020-02-14 01:15:35 -0800340 }
341
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800342 auto default_metadata_key_dir = data_rec->metadata_key_dir;
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700343 if (!user_devices.empty()) {
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800344 default_metadata_key_dir = default_metadata_key_dir + "/default";
345 }
Paul Crowley249c2fb2020-02-07 12:51:56 -0800346 auto gen = needs_encrypt ? makeGen(options) : neverGen();
Paul Crowley0fd26262018-01-30 09:48:19 -0800347 KeyBuffer key;
Jaegeuk Kimfb9aada2023-01-13 08:22:11 -0800348 if (!read_key(default_metadata_key_dir, gen, true, &key)) {
David Andersone1791572021-11-05 18:57:49 -0700349 LOG(ERROR) << "read_key failed in mountFstab";
350 return false;
351 }
Paul Lawrence4b140d32019-08-07 15:22:57 -0700352
Paul Crowleyd5759812016-06-02 11:04:27 -0700353 std::string crypto_blkdev;
Paul Crowley886e5722020-02-07 12:51:56 -0800354 uint64_t nr_sec;
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700355 if (!create_crypto_blk_dev(kDmNameUserdata, blk_device, key, options, &crypto_blkdev, &nr_sec,
356 true)) {
David Andersone1791572021-11-05 18:57:49 -0700357 LOG(ERROR) << "create_crypto_blk_dev failed in mountFstab";
Paul Crowley572c0242020-02-14 01:15:35 -0800358 return false;
David Andersone1791572021-11-05 18:57:49 -0700359 }
Paul Lawrence236e5e82019-06-25 14:44:33 -0700360
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700361 // create dm-default-key for user devices
362 std::vector<std::string> crypto_user_blkdev;
363 for (auto& device : user_devices) {
364 std::string name = Basename(device);
365 auto metadata_key_dir = data_rec->metadata_key_dir + "/" + name;
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800366
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700367 if (!read_key(metadata_key_dir, gen, false, &key)) {
368 LOG(ERROR) << "read_key failed with zoned device: " << device;
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800369 return false;
370 }
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700371 std::string crypto_blkdev_arg;
372 if (!create_crypto_blk_dev(name, device, key, options, &crypto_blkdev_arg, &nr_sec, true)) {
373 LOG(ERROR) << "fscrypt_mount_metadata_encrypted: failed with device: " << device;
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800374 return false;
375 }
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700376 crypto_user_blkdev.push_back(crypto_blkdev_arg.c_str());
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800377 }
378
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800379 if (needs_encrypt) {
Daniel Rosenberg358487a2024-07-29 13:48:38 -0700380 EncryptionInProgress marker(*data_rec);
381 if (!marker.Mark()) return false;
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800382 if (should_format) {
383 status_t error;
384
385 if (fs_type == "ext4") {
386 error = ext4::Format(crypto_blkdev, 0, mount_point);
387 } else if (fs_type == "f2fs") {
Ashok Mutyala8a398782024-05-28 09:31:44 +0000388 error = f2fs::Format(crypto_blkdev, is_zoned, crypto_user_blkdev, length);
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800389 } else {
390 LOG(ERROR) << "Unknown filesystem type: " << fs_type;
391 return false;
392 }
David Andersone1791572021-11-05 18:57:49 -0700393 if (error != 0) {
394 LOG(ERROR) << "Format of " << crypto_blkdev << " for " << mount_point
395 << " failed (err=" << error << ").";
396 return false;
397 }
398 LOG(DEBUG) << "Format of " << crypto_blkdev << " for " << mount_point << " succeeded.";
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800399 } else {
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700400 if (!user_devices.empty()) {
401 LOG(ERROR) << "encrypt_inplace cannot support zoned or userdata_exp device; should "
402 "format it.";
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800403 return false;
404 }
Eric Biggers640a1a92022-03-09 20:39:04 +0000405 if (!encrypt_inplace(crypto_blkdev, blk_device, nr_sec)) {
David Andersone1791572021-11-05 18:57:49 -0700406 LOG(ERROR) << "encrypt_inplace failed in mountFstab";
407 return false;
408 }
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800409 }
Daniel Rosenberg358487a2024-07-29 13:48:38 -0700410 if (!marker.Remove()) return false;
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800411 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700412
Paul Crowley0fd26262018-01-30 09:48:19 -0800413 LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
faqiang.zhudd20dc32021-07-19 12:27:37 +0800414 mount_via_fs_mgr(mount_point.c_str(), crypto_blkdev.c_str(), needs_encrypt);
Paul Crowley7fbd8d42020-03-23 08:59:12 -0700415
416 // Record that there's at least one fstab entry with metadata encryption
417 if (!android::base::SetProperty("ro.crypto.metadata.enabled", "true")) {
418 LOG(WARNING) << "failed to set ro.crypto.metadata.enabled"; // This isn't fatal
419 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700420 return true;
421}
Paul Crowley220567c2020-02-07 12:45:20 -0800422
Paul Crowley886e5722020-02-07 12:51:56 -0800423static bool get_volume_options(CryptoOptions* options) {
424 return parse_options(android::base::GetProperty("ro.crypto.volume.metadata.encryption", ""),
425 options);
426}
427
428bool defaultkey_volume_keygen(KeyGeneration* gen) {
429 CryptoOptions options;
430 if (!get_volume_options(&options)) return false;
431 *gen = makeGen(options);
432 return true;
433}
434
435bool defaultkey_setup_ext_volume(const std::string& label, const std::string& blk_device,
436 const KeyBuffer& key, std::string* out_crypto_blkdev) {
437 LOG(DEBUG) << "defaultkey_setup_ext_volume: " << label << " " << blk_device;
438
439 CryptoOptions options;
440 if (!get_volume_options(&options)) return false;
441 uint64_t nr_sec;
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700442 return create_crypto_blk_dev(label, blk_device, key, options, out_crypto_blkdev, &nr_sec,
443 false);
Paul Crowley886e5722020-02-07 12:51:56 -0800444}
445
Yo Chiang0af25a32020-10-07 14:20:00 +0800446bool destroy_dsu_metadata_key(const std::string& dsu_slot) {
447 LOG(DEBUG) << "destroy_dsu_metadata_key: " << dsu_slot;
448
449 const auto dsu_metadata_key_dir = android::gsi::GetDsuMetadataKeyDir(dsu_slot);
450 if (!pathExists(dsu_metadata_key_dir)) {
451 LOG(DEBUG) << "DSU metadata_key_dir doesn't exist, nothing to remove: "
452 << dsu_metadata_key_dir;
453 return true;
454 }
455
456 // Ensure that the DSU key directory is different from the host OS'.
457 // Under normal circumstances, this should never happen, but handle it just in case.
458 if (auto data_rec = GetEntryForMountPoint(&fstab_default, "/data")) {
459 if (dsu_metadata_key_dir == data_rec->metadata_key_dir) {
460 LOG(ERROR) << "DSU metadata_key_dir is same as host OS: " << dsu_metadata_key_dir;
461 return false;
462 }
463 }
464
465 bool ok = true;
466 for (auto suffix : {"/key", "/tmp"}) {
467 const auto key_path = dsu_metadata_key_dir + suffix;
468 if (pathExists(key_path)) {
469 LOG(DEBUG) << "Destroy key: " << key_path;
470 if (!android::vold::destroyKey(key_path)) {
471 LOG(ERROR) << "Failed to destroyKey(): " << key_path;
472 ok = false;
473 }
474 }
475 }
476 if (!ok) {
477 return false;
478 }
479
480 LOG(DEBUG) << "Remove DSU metadata_key_dir: " << dsu_metadata_key_dir;
481 // DeleteDirContentsAndDir() already logged any error, so don't log repeatedly.
482 return android::vold::DeleteDirContentsAndDir(dsu_metadata_key_dir) == android::OK;
483}
484
Paul Crowley220567c2020-02-07 12:45:20 -0800485} // namespace vold
486} // namespace android