blob: 19a90adbfa55d9c462803f92d2966bc6d2857e64 [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
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <algorithm>
Paul Crowleyd5759812016-06-02 11:04:27 -070021#include <string>
22#include <thread>
23#include <vector>
24
25#include <fcntl.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070026#include <sys/param.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080030#include <android-base/file.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070031#include <android-base/logging.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080032#include <android-base/properties.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070033#include <android-base/unique_fd.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080034#include <cutils/fs.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070035#include <fs_mgr.h>
David Andersonb9224732019-05-13 13:02:54 -070036#include <libdm/dm.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070037
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070038#include "Checkpoint.h"
Paul Crowley220567c2020-02-07 12:45:20 -080039#include "CryptoType.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070040#include "EncryptInplace.h"
41#include "KeyStorage.h"
42#include "KeyUtil.h"
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080043#include "Keymaster.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070044#include "Utils.h"
45#include "VoldUtil.h"
46
Paul Crowleyd5759812016-06-02 11:04:27 -070047#define TABLE_LOAD_RETRIES 10
Paul Crowleyd5759812016-06-02 11:04:27 -070048
Paul Crowley220567c2020-02-07 12:45:20 -080049namespace android {
50namespace vold {
51
Tom Cherry4c5bde22019-01-29 14:34:01 -080052using android::fs_mgr::FstabEntry;
53using android::fs_mgr::GetEntryForMountPoint;
Pavel Grafove2e2d302017-08-01 17:15:53 +010054using android::vold::KeyBuffer;
David Andersonb9224732019-05-13 13:02:54 -070055using namespace android::dm;
Pavel Grafove2e2d302017-08-01 17:15:53 +010056
Paul Crowley249c2fb2020-02-07 12:51:56 -080057// Parsed from metadata options
58struct 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 Crowleyd5759812016-06-02 11:04:27 -070064static const std::string kDmNameUserdata = "userdata";
65
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080066static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
67static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
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) {
84 return KeyGeneration{options.cipher.get_keysize(), true, false};
85}
86
Paul Crowleyd5759812016-06-02 11:04:27 -070087static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
Shawn Willden2b1ff5a2020-01-16 14:08:36 -070088 // 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 Crowleyd5759812016-06-02 11:04:27 -070096 // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
97 // partitions in the fsck domain.
LongPing Wei7f3ab952019-01-30 16:03:14 +080098 if (setexeccon(android::vold::sFsckContext)) {
Paul Crowleyd5759812016-06-02 11:04:27 -070099 PLOG(ERROR) << "Failed to setexeccon";
100 return false;
101 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800102 auto mount_rc = fs_mgr_do_mount(&fstab_default, const_cast<char*>(mount_point),
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700103 const_cast<char*>(blk_device), nullptr,
104 android::vold::cp_needsCheckpoint());
Paul Crowleyd5759812016-06-02 11:04:27 -0700105 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 Rosenberg690d6de2018-12-14 01:08:10 -0800117// 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 Kaiser8ae16db2018-12-18 11:10:31 -0800119static void commit_key(const std::string& dir) {
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800120 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 Crowley4eac2642020-02-12 11:04:05 -0800142static bool read_key(const std::string& metadata_key_dir, const KeyGeneration& gen,
143 KeyBuffer* key) {
Paul Crowley572c0242020-02-14 01:15:35 -0800144 if (metadata_key_dir.empty()) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800145 LOG(ERROR) << "Failed to get metadata_key_dir";
Paul Crowleyd5759812016-06-02 11:04:27 -0700146 return false;
147 }
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800148 std::string sKey;
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800149 auto dir = metadata_key_dir + "/key";
150 LOG(DEBUG) << "metadata_key_dir/key: " << dir;
Paul Crowley98a23a12018-05-09 13:01:16 -0700151 if (fs_mkdirs(dir.c_str(), 0700)) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800152 PLOG(ERROR) << "Creating directories: " << dir;
Paul Crowley98a23a12018-05-09 13:01:16 -0700153 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800154 }
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800155 auto temp = metadata_key_dir + "/tmp";
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800156 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 Crowley4eac2642020-02-12 11:04:05 -0800165 LOG(ERROR) << "Failed to read incomplete key: " << dir;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800166 else if (!keymaster.deleteKey(sKey))
Paul Crowley4eac2642020-02-12 11:04:05 -0800167 LOG(ERROR) << "Incomplete key deletion failed, continuing anyway: " << dir;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800168 else
169 unlink(newKeyPath.c_str());
170 }
171 bool needs_cp = cp_needsCheckpoint();
Paul Crowley4eac2642020-02-12 11:04:05 -0800172 if (!retrieveOrGenerateKey(dir, temp, kEmptyAuthentication, gen, key, needs_cp)) return false;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800173 if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach();
Paul Crowleyd5759812016-06-02 11:04:27 -0700174 return true;
175}
176
Paul Crowley14c8c072018-09-18 13:30:21 -0700177static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200178 if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700179 PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
180 return false;
181 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700182 return true;
183}
184
Paul Crowley572c0242020-02-14 01:15:35 -0800185static bool create_crypto_blk_dev(const std::string& dm_name, const std::string& blk_device,
Paul Crowley249c2fb2020-02-07 12:51:56 -0800186 const KeyBuffer& key, const CryptoOptions& options,
187 std::string* crypto_blkdev) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800188 uint64_t nr_sec;
Paul Crowley572c0242020-02-14 01:15:35 -0800189 if (!get_number_of_sectors(blk_device, &nr_sec)) return false;
Paul Crowley84e84c52020-01-29 16:09:19 -0800190
David Andersonb9224732019-05-13 13:02:54 -0700191 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 Crowleyd5759812016-06-02 11:04:27 -0700194 return false;
195 }
David Andersonb9224732019-05-13 13:02:54 -0700196 std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size());
Paul Crowleyd5759812016-06-02 11:04:27 -0700197
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800198 DmTable table;
Paul Crowley249c2fb2020-02-07 12:51:56 -0800199 table.Emplace<DmTargetDefaultKey>(0, nr_sec, options.cipher.get_kernel_name(), hex_key,
200 blk_device, 0, options.is_legacy, options.set_dun);
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800201
202 auto& dm = DeviceMapper::Instance();
Paul Crowley14c8c072018-09-18 13:30:21 -0700203 for (int i = 0;; i++) {
David Andersonb9224732019-05-13 13:02:54 -0700204 if (dm.CreateDevice(dm_name, table)) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700205 break;
206 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700207 if (i + 1 >= TABLE_LOAD_RETRIES) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800208 PLOG(ERROR) << "Could not create default-key device " << dm_name;
Paul Crowleyd5759812016-06-02 11:04:27 -0700209 return false;
210 }
David Andersonb9224732019-05-13 13:02:54 -0700211 PLOG(INFO) << "Could not create default-key device, retrying";
Paul Crowleyd5759812016-06-02 11:04:27 -0700212 usleep(500000);
213 }
214
David Andersonb9224732019-05-13 13:02:54 -0700215 if (!dm.GetDmDevicePathByName(dm_name, crypto_blkdev)) {
216 LOG(ERROR) << "Cannot retrieve default-key device status " << dm_name;
Paul Crowleyd5759812016-06-02 11:04:27 -0700217 return false;
218 }
219 return true;
220}
221
Paul Crowley249c2fb2020-02-07 12:51:56 -0800222static 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 Crowley572c0242020-02-14 01:15:35 -0800227 }
228 }
229 return invalid_crypto_type;
230}
231
Paul Crowley249c2fb2020-02-07 12:51:56 -0800232static 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 Crowley572c0242020-02-14 01:15:35 -0800237 }
Paul Crowley249c2fb2020-02-07 12:51:56 -0800238 return true;
Paul Crowley572c0242020-02-14 01:15:35 -0800239}
240
Paul Lawrence236e5e82019-06-25 14:44:33 -0700241bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point,
242 bool needs_encrypt) {
Eric Biggersa701c452018-10-23 13:06:55 -0700243 LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt;
Paul Crowley0fd26262018-01-30 09:48:19 -0800244 auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
Nikita Ioffef850e6e2019-12-09 21:19:11 +0000245 if (encrypted_state != "" && encrypted_state != "encrypted") {
Eric Biggersa701c452018-10-23 13:06:55 -0700246 LOG(DEBUG) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state;
Paul Crowleyd5759812016-06-02 11:04:27 -0700247 return false;
248 }
Tom Cherry4c5bde22019-01-29 14:34:01 -0800249
250 auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point);
Paul Crowleyd5759812016-06-02 11:04:27 -0700251 if (!data_rec) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800252 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 Crowleyd5759812016-06-02 11:04:27 -0700258 return false;
259 }
Paul Crowley572c0242020-02-14 01:15:35 -0800260
261 bool is_legacy;
262 if (!DmTargetDefaultKey::IsLegacy(&is_legacy)) return false;
263
Paul Crowley249c2fb2020-02-07 12:51:56 -0800264 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 Crowley572c0242020-02-14 01:15:35 -0800280 }
281
Paul Crowley249c2fb2020-02-07 12:51:56 -0800282 auto gen = needs_encrypt ? makeGen(options) : neverGen();
Paul Crowley0fd26262018-01-30 09:48:19 -0800283 KeyBuffer key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800284 if (!read_key(data_rec->metadata_key_dir, gen, &key)) return false;
Paul Lawrence4b140d32019-08-07 15:22:57 -0700285
Paul Crowleyd5759812016-06-02 11:04:27 -0700286 std::string crypto_blkdev;
Paul Crowley249c2fb2020-02-07 12:51:56 -0800287 if (!create_crypto_blk_dev(kDmNameUserdata, data_rec->blk_device, key, options, &crypto_blkdev))
Paul Crowley572c0242020-02-14 01:15:35 -0800288 return false;
Paul Lawrence236e5e82019-06-25 14:44:33 -0700289
Paul Crowley0fd26262018-01-30 09:48:19 -0800290 // FIXME handle the corrupt case
291 if (needs_encrypt) {
Paul Crowleyc9b92f02020-01-30 15:26:15 -0800292 uint64_t nr_sec;
293 if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800294 LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec;
295 off64_t size_already_done = 0;
Paul Lawrence236e5e82019-06-25 14:44:33 -0700296 auto rc = cryptfs_enable_inplace(crypto_blkdev.data(), blk_device.data(), nr_sec,
Tom Cherry4c5bde22019-01-29 14:34:01 -0800297 &size_already_done, nr_sec, 0, false);
Paul Crowley0fd26262018-01-30 09:48:19 -0800298 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 Crowleyd5759812016-06-02 11:04:27 -0700307 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700308
Paul Crowley0fd26262018-01-30 09:48:19 -0800309 LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
Tom Cherry4c5bde22019-01-29 14:34:01 -0800310 mount_via_fs_mgr(data_rec->mount_point.c_str(), crypto_blkdev.c_str());
Paul Crowleyd5759812016-06-02 11:04:27 -0700311 return true;
312}
Paul Crowley220567c2020-02-07 12:45:20 -0800313
314} // namespace vold
315} // namespace android