bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [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" |
| 18 | #include "KeyBuffer.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <string> |
| 22 | #include <thread> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/param.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include <linux/dm-ioctl.h> |
| 32 | |
| 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/properties.h> |
| 36 | #include <android-base/unique_fd.h> |
| 37 | #include <cutils/fs.h> |
| 38 | #include <fs_mgr.h> |
| 39 | |
| 40 | #include "Checkpoint.h" |
| 41 | #include "EncryptInplace.h" |
| 42 | #include "KeyStorage.h" |
| 43 | #include "KeyUtil.h" |
| 44 | #include "Keymaster.h" |
| 45 | #include "Utils.h" |
| 46 | #include "VoldUtil.h" |
| 47 | |
| 48 | #define DM_CRYPT_BUF_SIZE 4096 |
| 49 | #define TABLE_LOAD_RETRIES 10 |
| 50 | #define DEFAULT_KEY_TARGET_TYPE "default-key" |
| 51 | |
| 52 | using android::fs_mgr::FstabEntry; |
| 53 | using android::fs_mgr::GetEntryForMountPoint; |
| 54 | using android::fs_mgr::ReadDefaultFstab; |
| 55 | using android::vold::KeyBuffer; |
| 56 | |
| 57 | static const std::string kDmNameUserdata = "userdata"; |
| 58 | |
| 59 | static const char* kFn_keymaster_key_blob = "keymaster_key_blob"; |
| 60 | static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded"; |
| 61 | |
| 62 | static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) { |
| 63 | // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted |
| 64 | // partitions in the fsck domain. |
| 65 | if (setexeccon(android::vold::sFsckContext)) { |
| 66 | PLOG(ERROR) << "Failed to setexeccon"; |
| 67 | return false; |
| 68 | } |
| 69 | auto mount_rc = fs_mgr_do_mount(&fstab_default, const_cast<char*>(mount_point), |
| 70 | const_cast<char*>(blk_device), nullptr, |
| 71 | false); |
| 72 | if (setexeccon(nullptr)) { |
| 73 | PLOG(ERROR) << "Failed to clear setexeccon"; |
| 74 | return false; |
| 75 | } |
| 76 | if (mount_rc != 0) { |
| 77 | LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc; |
| 78 | return false; |
| 79 | } |
| 80 | LOG(DEBUG) << "Mounted " << mount_point; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | android::fs_mgr::Fstab fstab_default; |
| 85 | |
| 86 | namespace android { |
| 87 | namespace vold { |
| 88 | |
| 89 | // Note: It is possible to orphan a key if it is removed before deleting |
| 90 | // Update this once keymaster APIs change, and we have a proper commit. |
| 91 | static void commit_key(const std::string& dir) { |
| 92 | while (!android::base::WaitForProperty("vold.checkpoint_committed", "1")) { |
| 93 | LOG(ERROR) << "Wait for boot timed out"; |
| 94 | } |
| 95 | Keymaster keymaster; |
| 96 | auto keyPath = dir + "/" + kFn_keymaster_key_blob; |
| 97 | auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 98 | std::string key; |
| 99 | |
| 100 | if (!android::base::ReadFileToString(keyPath, &key)) { |
| 101 | LOG(ERROR) << "Failed to read old key: " << dir; |
| 102 | return; |
| 103 | } |
| 104 | if (rename(newKeyPath.c_str(), keyPath.c_str()) != 0) { |
| 105 | PLOG(ERROR) << "Unable to move upgraded key to location: " << keyPath; |
| 106 | return; |
| 107 | } |
| 108 | if (!keymaster.deleteKey(key)) { |
| 109 | LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir; |
| 110 | } |
| 111 | LOG(INFO) << "Old Key deleted: " << dir; |
| 112 | } |
| 113 | |
| 114 | static bool read_key(const FstabEntry& data_rec, bool create_if_absent, KeyBuffer* key) { |
| 115 | if (data_rec.key_dir.empty()) { |
| 116 | LOG(ERROR) << "Failed to get key_dir"; |
| 117 | return false; |
| 118 | } |
| 119 | std::string key_dir = data_rec.key_dir; |
| 120 | std::string sKey; |
| 121 | auto dir = key_dir + "/key"; |
| 122 | LOG(DEBUG) << "key_dir/key: " << dir; |
| 123 | if (fs_mkdirs(dir.c_str(), 0700)) { |
| 124 | PLOG(ERROR) << "Creating directories: " << dir; |
| 125 | return false; |
| 126 | } |
| 127 | auto temp = key_dir + "/tmp"; |
| 128 | auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded; |
| 129 | /* If we have a leftover upgraded key, delete it. |
| 130 | * We either failed an update and must return to the old key, |
| 131 | * or we rebooted before commiting the keys in a freak accident. |
| 132 | * Either way, we can re-upgrade the key if we need to. |
| 133 | */ |
| 134 | Keymaster keymaster; |
| 135 | if (pathExists(newKeyPath)) { |
| 136 | if (!android::base::ReadFileToString(newKeyPath, &sKey)) |
| 137 | LOG(ERROR) << "Failed to read old key: " << dir; |
| 138 | else if (!keymaster.deleteKey(sKey)) |
| 139 | LOG(ERROR) << "Old key deletion failed, continuing anyway: " << dir; |
| 140 | else |
| 141 | unlink(newKeyPath.c_str()); |
| 142 | } |
| 143 | // bool needs_cp = cp_needsCheckpoint(); |
| 144 | bool needs_cp = false; |
| 145 | if (!android::vold::retrieveKey(create_if_absent, dir, temp, key, needs_cp)) return false; |
| 146 | if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach(); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | } // namespace vold |
| 151 | } // namespace android |
| 152 | |
| 153 | static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) { |
| 154 | KeyBuffer hex_key; |
| 155 | if (android::vold::StrToHex(key, hex_key) != android::OK) { |
| 156 | LOG(ERROR) << "Failed to turn key to hex"; |
| 157 | return KeyBuffer(); |
| 158 | } |
| 159 | auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0"; |
| 160 | return res; |
| 161 | } |
| 162 | |
| 163 | static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) { |
| 164 | if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) { |
| 165 | PLOG(ERROR) << "Unable to measure size of " << real_blkdev; |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | static struct dm_ioctl* dm_ioctl_init(char* buffer, size_t buffer_size, const std::string& dm_name) { |
| 172 | if (buffer_size < sizeof(dm_ioctl)) { |
| 173 | LOG(ERROR) << "dm_ioctl buffer too small"; |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | memset(buffer, 0, buffer_size); |
| 178 | struct dm_ioctl* io = (struct dm_ioctl*)buffer; |
| 179 | io->data_size = buffer_size; |
| 180 | io->data_start = sizeof(struct dm_ioctl); |
| 181 | io->version[0] = 4; |
| 182 | io->version[1] = 0; |
| 183 | io->version[2] = 0; |
| 184 | io->flags = 0; |
| 185 | dm_name.copy(io->name, sizeof(io->name)); |
| 186 | return io; |
| 187 | } |
| 188 | |
| 189 | static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec, |
| 190 | const std::string& target_type, const KeyBuffer& crypt_params, |
| 191 | std::string* crypto_blkdev) { |
| 192 | android::base::unique_fd dm_fd( |
| 193 | TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC, 0))); |
| 194 | if (dm_fd == -1) { |
| 195 | PLOG(ERROR) << "Cannot open device-mapper"; |
| 196 | return false; |
| 197 | } |
| 198 | alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
| 199 | auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 200 | if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) { |
| 201 | PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // Get the device status, in particular, the name of its device file |
| 206 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 207 | if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) { |
| 208 | PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name; |
| 209 | return false; |
| 210 | } |
| 211 | *crypto_blkdev = std::string() + "/dev/block/dm-" + |
| 212 | std::to_string((io->dev & 0xff) | ((io->dev >> 12) & 0xfff00)); |
| 213 | |
| 214 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 215 | size_t paramix = io->data_start + sizeof(struct dm_target_spec); |
| 216 | size_t nullix = paramix + crypt_params.size(); |
| 217 | size_t endix = (nullix + 1 + 7) & 8; // Add room for \0 and align to 8 byte boundary |
| 218 | |
| 219 | if (endix > sizeof(buffer)) { |
| 220 | LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE"; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | io->target_count = 1; |
| 225 | auto tgt = (struct dm_target_spec*)(buffer + io->data_start); |
| 226 | tgt->status = 0; |
| 227 | tgt->sector_start = 0; |
| 228 | tgt->length = nr_sec; |
| 229 | target_type.copy(tgt->target_type, sizeof(tgt->target_type)); |
| 230 | memcpy(buffer + paramix, crypt_params.data(), |
| 231 | std::min(crypt_params.size(), sizeof(buffer) - paramix)); |
| 232 | buffer[nullix] = '\0'; |
| 233 | tgt->next = endix; |
| 234 | |
| 235 | for (int i = 0;; i++) { |
| 236 | if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) { |
| 237 | break; |
| 238 | } |
| 239 | if (i + 1 >= TABLE_LOAD_RETRIES) { |
| 240 | PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed"; |
| 241 | return false; |
| 242 | } |
| 243 | PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying"; |
| 244 | usleep(500000); |
| 245 | } |
| 246 | |
| 247 | // Resume this device to activate it |
| 248 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 249 | if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) { |
| 250 | PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name; |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point, |
| 257 | bool needs_encrypt) { |
| 258 | LOG(ERROR) << "fscrypt_mount_metadata_encrypted: " << blk_device << " " << mount_point << " " << needs_encrypt; |
| 259 | // auto encrypted_state = android::base::GetProperty("ro.crypto.state", ""); |
| 260 | // if (encrypted_state != "") { |
| 261 | // LOG(ERROR) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state; |
| 262 | // return false; |
| 263 | // } |
| 264 | |
| 265 | if (!ReadDefaultFstab(&fstab_default)) { |
| 266 | PLOG(ERROR) << "Failed to open default fstab"; |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point); |
| 271 | if (!data_rec) { |
| 272 | LOG(ERROR) << "Failed to get data_rec"; |
| 273 | return false; |
| 274 | } |
| 275 | KeyBuffer key; |
| 276 | if (!read_key(*data_rec, needs_encrypt, &key)) return false; |
| 277 | uint64_t nr_sec; |
| 278 | if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false; |
| 279 | std::string crypto_blkdev; |
| 280 | if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE, |
| 281 | default_key_params(blk_device, key), &crypto_blkdev)) |
| 282 | return false; |
| 283 | // FIXME handle the corrupt case |
| 284 | if (needs_encrypt) { |
| 285 | LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec; |
| 286 | off64_t size_already_done = 0; |
| 287 | auto rc = cryptfs_enable_inplace(crypto_blkdev.data(), blk_device.data(), nr_sec, |
| 288 | &size_already_done, nr_sec, 0, false); |
| 289 | if (rc != 0) { |
| 290 | LOG(ERROR) << "Inplace crypto failed with code: " << rc; |
| 291 | return false; |
| 292 | } |
| 293 | if (static_cast<uint64_t>(size_already_done) != nr_sec) { |
| 294 | LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done; |
| 295 | return false; |
| 296 | } |
| 297 | LOG(INFO) << "Inplace encryption complete"; |
| 298 | } |
| 299 | |
| 300 | LOG(ERROR) << "Mounting metadata-encrypted filesystem:" << mount_point; |
| 301 | mount_via_fs_mgr(data_rec->mount_point.c_str(), crypto_blkdev.c_str()); |
| 302 | android::base::SetProperty("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
| 303 | return true; |
| 304 | } |