Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [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 "KeyBuffer.h" |
| 18 | #include "MetadataCrypt.h" |
| 19 | |
| 20 | #include <string> |
| 21 | #include <thread> |
| 22 | #include <vector> |
| 23 | #include <algorithm> |
| 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/logging.h> |
| 34 | #include <android-base/properties.h> |
| 35 | #include <android-base/unique_fd.h> |
| 36 | #include <cutils/fs.h> |
| 37 | //#include <fs_mgr.h> |
| 38 | |
| 39 | //#include "EncryptInplace.h" |
| 40 | #include "KeyStorage4.h" |
| 41 | #include "KeyUtil.h" |
| 42 | //#include "secontext.h" |
| 43 | #include "Utils.h" |
| 44 | //#include "VoldUtil.h" |
| 45 | |
| 46 | #include <iostream> |
| 47 | #define LOG(x) std::cout |
| 48 | #define PLOG(x) std::cout |
| 49 | #include <linux/fs.h> |
| 50 | |
| 51 | #define DM_CRYPT_BUF_SIZE 4096 |
| 52 | #define TABLE_LOAD_RETRIES 10 |
| 53 | #define DEFAULT_KEY_TARGET_TYPE "default-key" |
| 54 | |
| 55 | using android::vold::KeyBuffer; |
| 56 | |
| 57 | static const std::string kDmNameUserdata = "userdata"; |
| 58 | |
| 59 | void get_blkdev_size(int fd, unsigned long* nr_sec) { |
| 60 | if ((ioctl(fd, BLKGETSIZE, nr_sec)) == -1) { |
| 61 | *nr_sec = 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static const char* kLookup = "0123456789abcdef"; |
| 66 | |
| 67 | android::status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex) { |
| 68 | hex.clear(); |
| 69 | for (size_t i = 0; i < str.size(); i++) { |
| 70 | hex.push_back(kLookup[(str.data()[i] & 0xF0) >> 4]); |
| 71 | hex.push_back(kLookup[str.data()[i] & 0x0F]); |
| 72 | } |
| 73 | return android::OK; |
| 74 | } |
| 75 | |
| 76 | /*static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) { |
| 77 | // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted |
| 78 | // partitions in the fsck domain. |
| 79 | if (setexeccon(secontextFsck())) { |
| 80 | PLOG(ERROR) << "Failed to setexeccon"; |
| 81 | return false; |
| 82 | } |
| 83 | auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point), |
| 84 | const_cast<char*>(blk_device), nullptr); |
| 85 | if (setexeccon(nullptr)) { |
| 86 | PLOG(ERROR) << "Failed to clear setexeccon"; |
| 87 | return false; |
| 88 | } |
| 89 | if (mount_rc != 0) { |
| 90 | LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc; |
| 91 | return false; |
| 92 | } |
| 93 | LOG(DEBUG) << "Mounted " << mount_point; |
| 94 | return true; |
| 95 | }*/ |
| 96 | |
| 97 | static bool read_key(const std::string& key_dir, bool create_if_absent, KeyBuffer* key) { |
| 98 | /*if (!data_rec->key_dir) { |
| 99 | LOG(ERROR) << "Failed to get key_dir"; |
| 100 | return false; |
| 101 | } |
| 102 | std::string key_dir = data_rec->key_dir;*/ |
| 103 | auto dir = key_dir + "/key"; |
| 104 | LOG(DEBUG) << "key_dir/key: " << dir << "\n"; |
| 105 | /*if (fs_mkdirs(dir.c_str(), 0700)) { |
| 106 | PLOG(ERROR) << "Creating directories: " << dir; |
| 107 | return false; |
| 108 | }*/ |
| 109 | auto temp = key_dir + "/tmp"; |
| 110 | if (!android::vold::retrieveKey(create_if_absent, dir, temp, key)) return false; |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) { |
| 115 | KeyBuffer hex_key; |
| 116 | if (/*android::vold::*/StrToHex(key, hex_key) != android::OK) { |
| 117 | LOG(ERROR) << "Failed to turn key to hex\n"; |
| 118 | return KeyBuffer(); |
| 119 | } |
| 120 | auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0"; |
| 121 | return res; |
| 122 | } |
| 123 | |
| 124 | static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t *nr_sec) { |
| 125 | android::base::unique_fd dev_fd(TEMP_FAILURE_RETRY(open( |
| 126 | real_blkdev.c_str(), O_RDONLY | O_CLOEXEC, 0))); |
| 127 | if (dev_fd == -1) { |
| 128 | PLOG(ERROR) << "Unable to open " << real_blkdev << " to measure size\n"; |
| 129 | return false; |
| 130 | } |
| 131 | unsigned long res; |
| 132 | // TODO: should use BLKGETSIZE64 |
| 133 | get_blkdev_size(dev_fd.get(), &res); |
| 134 | if (res == 0) { |
| 135 | PLOG(ERROR) << "Unable to measure size of " << real_blkdev << "\n"; |
| 136 | return false; |
| 137 | } |
| 138 | *nr_sec = res; |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | static struct dm_ioctl* dm_ioctl_init(char *buffer, size_t buffer_size, |
| 143 | const std::string& dm_name) { |
| 144 | if (buffer_size < sizeof(dm_ioctl)) { |
| 145 | LOG(ERROR) << "dm_ioctl buffer too small\n"; |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
| 149 | memset(buffer, 0, buffer_size); |
| 150 | struct dm_ioctl* io = (struct dm_ioctl*) buffer; |
| 151 | io->data_size = buffer_size; |
| 152 | io->data_start = sizeof(struct dm_ioctl); |
| 153 | io->version[0] = 4; |
| 154 | io->version[1] = 0; |
| 155 | io->version[2] = 0; |
| 156 | io->flags = 0; |
| 157 | dm_name.copy(io->name, sizeof(io->name)); |
| 158 | return io; |
| 159 | } |
| 160 | |
| 161 | static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec, |
| 162 | const std::string& target_type, const KeyBuffer& crypt_params, |
| 163 | std::string* crypto_blkdev) { |
| 164 | PLOG(INFO) << "starting create_crypto_blk_dev\n"; |
| 165 | android::base::unique_fd dm_fd(TEMP_FAILURE_RETRY(open( |
| 166 | "/dev/device-mapper", O_RDWR | O_CLOEXEC, 0))); |
| 167 | if (dm_fd == -1) { |
| 168 | PLOG(ERROR) << "Cannot open device-mapper\n"; |
| 169 | return false; |
| 170 | } |
| 171 | alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
| 172 | auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 173 | if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) { |
| 174 | PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name << "\n"; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Get the device status, in particular, the name of its device file |
| 179 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 180 | if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) { |
| 181 | PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name << "\n"; |
| 182 | return false; |
| 183 | } |
| 184 | *crypto_blkdev = std::string() + "/dev/block/dm-" + std::to_string( |
| 185 | (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00)); |
| 186 | |
| 187 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 188 | size_t paramix = io->data_start + sizeof(struct dm_target_spec); |
| 189 | size_t nullix = paramix + crypt_params.size(); |
| 190 | size_t endix = (nullix + 1 + 7) & 8; // Add room for \0 and align to 8 byte boundary |
| 191 | |
| 192 | if (endix > sizeof(buffer)) { |
| 193 | LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE\n"; |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | io->target_count = 1; |
| 198 | auto tgt = (struct dm_target_spec *) (buffer + io->data_start); |
| 199 | tgt->status = 0; |
| 200 | tgt->sector_start = 0; |
| 201 | tgt->length = nr_sec; |
| 202 | target_type.copy(tgt->target_type, sizeof(tgt->target_type)); |
| 203 | memcpy(buffer + paramix, crypt_params.data(), |
| 204 | std::min(crypt_params.size(), sizeof(buffer) - paramix)); |
| 205 | buffer[nullix] = '\0'; |
| 206 | tgt->next = endix; |
| 207 | |
| 208 | for (int i = 0; ; i++) { |
| 209 | if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) { |
| 210 | break; |
| 211 | } |
| 212 | if (i+1 >= TABLE_LOAD_RETRIES) { |
| 213 | PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed\n"; |
| 214 | return false; |
| 215 | } |
| 216 | PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying\n"; |
| 217 | usleep(500000); |
| 218 | } |
| 219 | |
| 220 | // Resume this device to activate it |
| 221 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 222 | if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) { |
| 223 | PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name << "\n"; |
| 224 | return false; |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool e4crypt_mount_metadata_encrypted(const std::string& mount_point, bool needs_encrypt, const std::string& key_dir, const std::string& blk_device, std::string* crypto_blkdev) { |
| 230 | LOG(DEBUG) << "e4crypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt << "\n"; |
| 231 | /*auto encrypted_state = android::base::GetProperty("ro.crypto.state", ""); |
| 232 | if (encrypted_state != "") { |
| 233 | LOG(DEBUG) << "e4crypt_enable_crypto got unexpected starting state: " << encrypted_state; |
| 234 | return false; |
| 235 | } |
| 236 | auto data_rec = fs_mgr_get_entry_for_mount_point(fstab_default, mount_point); |
| 237 | if (!data_rec) { |
| 238 | LOG(ERROR) << "Failed to get data_rec"; |
| 239 | return false; |
| 240 | }*/ |
| 241 | KeyBuffer key; |
| 242 | if (!read_key(key_dir, needs_encrypt, &key)) return false; |
| 243 | uint64_t nr_sec; |
| 244 | if (!get_number_of_sectors(blk_device, &nr_sec)) return false; |
| 245 | //std::string crypto_blkdev; |
| 246 | if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE, |
| 247 | default_key_params(blk_device, key), /*&*/crypto_blkdev)) |
| 248 | return false; |
| 249 | // FIXME handle the corrupt case |
| 250 | /*if (needs_encrypt) { |
| 251 | LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec; |
| 252 | off64_t size_already_done = 0; |
| 253 | auto rc = |
| 254 | cryptfs_enable_inplace(const_cast<char*>(crypto_blkdev.c_str()), data_rec->blk_device, |
| 255 | nr_sec, &size_already_done, nr_sec, 0, false); |
| 256 | if (rc != 0) { |
| 257 | LOG(ERROR) << "Inplace crypto failed with code: " << rc; |
| 258 | return false; |
| 259 | } |
| 260 | if (static_cast<uint64_t>(size_already_done) != nr_sec) { |
| 261 | LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done; |
| 262 | return false; |
| 263 | } |
| 264 | LOG(INFO) << "Inplace encryption complete"; |
| 265 | } |
| 266 | |
| 267 | LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point; |
| 268 | mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str());*/ |
| 269 | LOG(DEBUG) << "crypto block device '" << *crypto_blkdev << "\n"; |
| 270 | return true; |
| 271 | } |