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