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 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 17 | #include "KeyBuffer.h" |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 18 | #include "MetadataCrypt.h" |
| 19 | |
| 20 | #include <string> |
| 21 | #include <thread> |
| 22 | #include <vector> |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 23 | #include <algorithm> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 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 | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 34 | #include <android-base/unique_fd.h> |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 35 | #include <cutils/properties.h> |
| 36 | #include <fs_mgr.h> |
| 37 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 38 | #include "EncryptInplace.h" |
| 39 | #include "KeyStorage.h" |
| 40 | #include "KeyUtil.h" |
| 41 | #include "secontext.h" |
| 42 | #include "Utils.h" |
| 43 | #include "VoldUtil.h" |
| 44 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 45 | #define DM_CRYPT_BUF_SIZE 4096 |
| 46 | #define TABLE_LOAD_RETRIES 10 |
| 47 | #define DEFAULT_KEY_TARGET_TYPE "default-key" |
| 48 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 49 | using android::vold::KeyBuffer; |
| 50 | |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 51 | static const std::string kDmNameUserdata = "userdata"; |
| 52 | |
| 53 | static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) { |
| 54 | // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted |
| 55 | // partitions in the fsck domain. |
| 56 | if (setexeccon(secontextFsck())) { |
| 57 | PLOG(ERROR) << "Failed to setexeccon"; |
| 58 | return false; |
| 59 | } |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 60 | auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point), |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 61 | const_cast<char*>(blk_device), nullptr); |
| 62 | if (setexeccon(nullptr)) { |
| 63 | PLOG(ERROR) << "Failed to clear setexeccon"; |
| 64 | return false; |
| 65 | } |
| 66 | if (mount_rc != 0) { |
| 67 | LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc; |
| 68 | return false; |
| 69 | } |
| 70 | LOG(DEBUG) << "Mounted " << mount_point; |
| 71 | return true; |
| 72 | } |
| 73 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 74 | static bool read_key(bool create_if_absent, KeyBuffer* key) { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 75 | auto data_rec = fs_mgr_get_crypt_entry(fstab_default); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 76 | if (!data_rec) { |
| 77 | LOG(ERROR) << "Failed to get data_rec"; |
| 78 | return false; |
| 79 | } |
| 80 | if (!data_rec->key_dir) { |
| 81 | LOG(ERROR) << "Failed to get key_dir"; |
| 82 | return false; |
| 83 | } |
| 84 | LOG(DEBUG) << "key_dir: " << data_rec->key_dir; |
| 85 | if (!android::vold::pathExists(data_rec->key_dir)) { |
| 86 | if (mkdir(data_rec->key_dir, 0777) != 0) { |
| 87 | PLOG(ERROR) << "Unable to create: " << data_rec->key_dir; |
| 88 | return false; |
| 89 | } |
| 90 | LOG(DEBUG) << "Created: " << data_rec->key_dir; |
| 91 | } |
| 92 | std::string key_dir = data_rec->key_dir; |
| 93 | auto dir = key_dir + "/key"; |
| 94 | auto temp = key_dir + "/tmp"; |
| 95 | if (!android::vold::retrieveKey(create_if_absent, dir, temp, key)) return false; |
| 96 | return true; |
| 97 | } |
| 98 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 99 | static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) { |
| 100 | KeyBuffer hex_key; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 101 | if (android::vold::StrToHex(key, hex_key) != android::OK) { |
| 102 | LOG(ERROR) << "Failed to turn key to hex"; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 103 | return KeyBuffer(); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 104 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 105 | auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0"; |
| 106 | LOG(DEBUG) << "crypt_params: " << std::string(res.data(), res.size()); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 107 | return res; |
| 108 | } |
| 109 | |
| 110 | static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t *nr_sec) { |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 111 | android::base::unique_fd dev_fd(TEMP_FAILURE_RETRY(open( |
| 112 | real_blkdev.c_str(), O_RDONLY | O_CLOEXEC, 0))); |
| 113 | if (dev_fd == -1) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 114 | PLOG(ERROR) << "Unable to open " << real_blkdev << " to measure size"; |
| 115 | return false; |
| 116 | } |
| 117 | unsigned long res; |
| 118 | // TODO: should use BLKGETSIZE64 |
| 119 | get_blkdev_size(dev_fd.get(), &res); |
| 120 | if (res == 0) { |
| 121 | PLOG(ERROR) << "Unable to measure size of " << real_blkdev; |
| 122 | return false; |
| 123 | } |
| 124 | *nr_sec = res; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | static struct dm_ioctl* dm_ioctl_init(char *buffer, size_t buffer_size, |
| 129 | const std::string& dm_name) { |
| 130 | if (buffer_size < sizeof(dm_ioctl)) { |
| 131 | LOG(ERROR) << "dm_ioctl buffer too small"; |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
| 135 | memset(buffer, 0, buffer_size); |
| 136 | struct dm_ioctl* io = (struct dm_ioctl*) buffer; |
| 137 | io->data_size = buffer_size; |
| 138 | io->data_start = sizeof(struct dm_ioctl); |
| 139 | io->version[0] = 4; |
| 140 | io->version[1] = 0; |
| 141 | io->version[2] = 0; |
| 142 | io->flags = 0; |
| 143 | dm_name.copy(io->name, sizeof(io->name)); |
| 144 | return io; |
| 145 | } |
| 146 | |
| 147 | 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] | 148 | const std::string& target_type, const KeyBuffer& crypt_params, |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 149 | std::string* crypto_blkdev) { |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 150 | android::base::unique_fd dm_fd(TEMP_FAILURE_RETRY(open( |
| 151 | "/dev/device-mapper", O_RDWR | O_CLOEXEC, 0))); |
| 152 | if (dm_fd == -1) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 153 | PLOG(ERROR) << "Cannot open device-mapper"; |
| 154 | return false; |
| 155 | } |
| 156 | alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
| 157 | auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 158 | if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) { |
| 159 | PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name; |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Get the device status, in particular, the name of its device file |
| 164 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 165 | if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) { |
| 166 | PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name; |
| 167 | return false; |
| 168 | } |
| 169 | *crypto_blkdev = std::string() + "/dev/block/dm-" + std::to_string( |
| 170 | (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00)); |
| 171 | |
| 172 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 173 | size_t paramix = io->data_start + sizeof(struct dm_target_spec); |
| 174 | size_t nullix = paramix + crypt_params.size(); |
| 175 | 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] | 176 | |
| 177 | if (endix > sizeof(buffer)) { |
| 178 | LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE"; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | io->target_count = 1; |
| 183 | auto tgt = (struct dm_target_spec *) (buffer + io->data_start); |
| 184 | tgt->status = 0; |
| 185 | tgt->sector_start = 0; |
| 186 | tgt->length = nr_sec; |
| 187 | target_type.copy(tgt->target_type, sizeof(tgt->target_type)); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 188 | memcpy(buffer + paramix, crypt_params.data(), |
| 189 | std::min(crypt_params.size(), sizeof(buffer) - paramix)); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 190 | buffer[nullix] = '\0'; |
| 191 | tgt->next = endix; |
| 192 | |
| 193 | for (int i = 0; ; i++) { |
| 194 | if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) { |
| 195 | break; |
| 196 | } |
| 197 | if (i+1 >= TABLE_LOAD_RETRIES) { |
| 198 | PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed"; |
| 199 | return false; |
| 200 | } |
| 201 | PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying"; |
| 202 | usleep(500000); |
| 203 | } |
| 204 | |
| 205 | // Resume this device to activate it |
| 206 | io = dm_ioctl_init(buffer, sizeof(buffer), dm_name); |
| 207 | if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) { |
| 208 | PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name; |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | #define DATA_PREP_TIMEOUT 1000 |
| 215 | static bool prep_data_fs(void) |
| 216 | { |
| 217 | // NOTE: post_fs_data results in init calling back around to vold, so all |
| 218 | // callers to this method must be async |
| 219 | |
| 220 | /* Do the prep of the /data filesystem */ |
Guang Zhu | 5b6c6a2 | 2017-04-28 23:56:45 +0000 | [diff] [blame] | 221 | property_set("vold.post_fs_data_done", "0"); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 222 | property_set("vold.decrypt", "trigger_post_fs_data"); |
Guang Zhu | 5b6c6a2 | 2017-04-28 23:56:45 +0000 | [diff] [blame] | 223 | LOG(DEBUG) << "Waiting for post_fs_data_done"; |
| 224 | |
| 225 | /* Wait a max of 50 seconds, hopefully it takes much less */ |
| 226 | for (int i = 0; ; i++) { |
| 227 | char p[PROPERTY_VALUE_MAX]; |
| 228 | |
| 229 | property_get("vold.post_fs_data_done", p, "0"); |
| 230 | if (*p == '1') { |
| 231 | LOG(INFO) << "Successful data prep"; |
| 232 | return true; |
| 233 | } |
| 234 | if (i + 1 == DATA_PREP_TIMEOUT) { |
| 235 | LOG(ERROR) << "post_fs_data timed out"; |
| 236 | return false; |
| 237 | } |
| 238 | usleep(50000); |
| 239 | } |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void async_kick_off() { |
| 243 | LOG(DEBUG) << "Asynchronously restarting framework"; |
Guang Zhu | 5b6c6a2 | 2017-04-28 23:56:45 +0000 | [diff] [blame] | 244 | sleep(2); // TODO: this mirrors cryptfs, but can it be made shorter? |
| 245 | property_set("vold.decrypt", "trigger_load_persist_props"); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 246 | if (!prep_data_fs()) return; |
Guang Zhu | 5b6c6a2 | 2017-04-28 23:56:45 +0000 | [diff] [blame] | 247 | /* startup service classes main and late_start */ |
| 248 | property_set("vold.decrypt", "trigger_restart_framework"); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | bool e4crypt_mount_metadata_encrypted() { |
| 252 | LOG(DEBUG) << "e4crypt_mount_default_encrypted"; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 253 | KeyBuffer key; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 254 | if (!read_key(false, &key)) return false; |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 255 | auto data_rec = fs_mgr_get_crypt_entry(fstab_default); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 256 | if (!data_rec) { |
| 257 | LOG(ERROR) << "Failed to get data_rec"; |
| 258 | return false; |
| 259 | } |
| 260 | uint64_t nr_sec; |
| 261 | if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false; |
| 262 | std::string crypto_blkdev; |
| 263 | if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE, |
| 264 | default_key_params(data_rec->blk_device, key), &crypto_blkdev)) return false; |
| 265 | // FIXME handle the corrupt case |
| 266 | |
| 267 | LOG(DEBUG) << "Restarting filesystem for metadata encryption"; |
| 268 | mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str()); |
| 269 | std::thread(&async_kick_off).detach(); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool e4crypt_enable_crypto() { |
| 274 | LOG(DEBUG) << "e4crypt_enable_crypto"; |
| 275 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 276 | property_get("ro.crypto.state", encrypted_state, ""); |
| 277 | if (strcmp(encrypted_state, "")) { |
| 278 | LOG(DEBUG) << "e4crypt_enable_crypto got unexpected starting state: " << encrypted_state; |
| 279 | return false; |
| 280 | } |
| 281 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 282 | KeyBuffer key_ref; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 283 | if (!read_key(true, &key_ref)) return false; |
| 284 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 285 | auto data_rec = fs_mgr_get_crypt_entry(fstab_default); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 286 | if (!data_rec) { |
| 287 | LOG(ERROR) << "Failed to get data_rec"; |
| 288 | return false; |
| 289 | } |
| 290 | uint64_t nr_sec; |
| 291 | if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false; |
| 292 | |
| 293 | std::string crypto_blkdev; |
| 294 | if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE, |
| 295 | default_key_params(data_rec->blk_device, key_ref), &crypto_blkdev)) return false; |
| 296 | |
| 297 | LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec; |
| 298 | off64_t size_already_done = 0; |
| 299 | auto rc = cryptfs_enable_inplace(const_cast<char *>(crypto_blkdev.c_str()), |
| 300 | data_rec->blk_device, nr_sec, &size_already_done, nr_sec, 0); |
| 301 | if (rc != 0) { |
| 302 | LOG(ERROR) << "Inplace crypto failed with code: " << rc; |
| 303 | return false; |
| 304 | } |
| 305 | if (static_cast<uint64_t>(size_already_done) != nr_sec) { |
| 306 | LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done; |
| 307 | return false; |
| 308 | } |
| 309 | LOG(INFO) << "Inplace encryption complete"; |
| 310 | |
| 311 | property_set("ro.crypto.state", "encrypted"); |
| 312 | property_set("ro.crypto.type", "file"); |
| 313 | |
| 314 | mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str()); |
Guang Zhu | 5b6c6a2 | 2017-04-28 23:56:45 +0000 | [diff] [blame] | 315 | property_set("vold.decrypt", "trigger_reset_main"); |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 316 | std::thread(&async_kick_off).detach(); |
| 317 | return true; |
| 318 | } |