blob: d31419fe0b28771cfb5b5ac7a389ee06bb419a14 [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>
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
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080033#include <android-base/file.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070034#include <android-base/logging.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080035#include <android-base/properties.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070036#include <android-base/unique_fd.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080037#include <cutils/fs.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070038#include <fs_mgr.h>
39
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070040#include "Checkpoint.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070041#include "EncryptInplace.h"
42#include "KeyStorage.h"
43#include "KeyUtil.h"
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080044#include "Keymaster.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070045#include "Utils.h"
46#include "VoldUtil.h"
47
Paul Crowleyd5759812016-06-02 11:04:27 -070048#define DM_CRYPT_BUF_SIZE 4096
49#define TABLE_LOAD_RETRIES 10
50#define DEFAULT_KEY_TARGET_TYPE "default-key"
51
Pavel Grafove2e2d302017-08-01 17:15:53 +010052using android::vold::KeyBuffer;
53
Paul Crowleyd5759812016-06-02 11:04:27 -070054static const std::string kDmNameUserdata = "userdata";
55
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080056static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
57static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
58
Paul Crowleyd5759812016-06-02 11:04:27 -070059static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
60 // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
61 // partitions in the fsck domain.
LongPing Wei7f3ab952019-01-30 16:03:14 +080062 if (setexeccon(android::vold::sFsckContext)) {
Paul Crowleyd5759812016-06-02 11:04:27 -070063 PLOG(ERROR) << "Failed to setexeccon";
64 return false;
65 }
Paul Crowleye2ee1522017-09-26 14:05:26 -070066 auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070067 const_cast<char*>(blk_device), nullptr,
68 android::vold::cp_needsCheckpoint());
Paul Crowleyd5759812016-06-02 11:04:27 -070069 if (setexeccon(nullptr)) {
70 PLOG(ERROR) << "Failed to clear setexeccon";
71 return false;
72 }
73 if (mount_rc != 0) {
74 LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc;
75 return false;
76 }
77 LOG(DEBUG) << "Mounted " << mount_point;
78 return true;
79}
80
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080081namespace android {
82namespace vold {
83
84// Note: It is possible to orphan a key if it is removed before deleting
85// Update this once keymaster APIs change, and we have a proper commit.
Greg Kaiser8ae16db2018-12-18 11:10:31 -080086static void commit_key(const std::string& dir) {
Daniel Rosenberg690d6de2018-12-14 01:08:10 -080087 while (!android::base::WaitForProperty("vold.checkpoint_committed", "1")) {
88 LOG(ERROR) << "Wait for boot timed out";
89 }
90 Keymaster keymaster;
91 auto keyPath = dir + "/" + kFn_keymaster_key_blob;
92 auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
93 std::string key;
94
95 if (!android::base::ReadFileToString(keyPath, &key)) {
96 LOG(ERROR) << "Failed to read old key: " << dir;
97 return;
98 }
99 if (rename(newKeyPath.c_str(), keyPath.c_str()) != 0) {
100 PLOG(ERROR) << "Unable to move upgraded key to location: " << keyPath;
101 return;
102 }
103 if (!keymaster.deleteKey(key)) {
104 LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
105 }
106 LOG(INFO) << "Old Key deleted: " << dir;
107}
108
Paul Crowley0fd26262018-01-30 09:48:19 -0800109static bool read_key(struct fstab_rec const* data_rec, bool create_if_absent, KeyBuffer* key) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700110 if (!data_rec->key_dir) {
111 LOG(ERROR) << "Failed to get key_dir";
112 return false;
113 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700114 std::string key_dir = data_rec->key_dir;
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800115 std::string sKey;
Paul Crowleyd5759812016-06-02 11:04:27 -0700116 auto dir = key_dir + "/key";
Paul Crowley98a23a12018-05-09 13:01:16 -0700117 LOG(DEBUG) << "key_dir/key: " << dir;
118 if (fs_mkdirs(dir.c_str(), 0700)) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800119 PLOG(ERROR) << "Creating directories: " << dir;
Paul Crowley98a23a12018-05-09 13:01:16 -0700120 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800121 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700122 auto temp = key_dir + "/tmp";
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800123 auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
124 /* If we have a leftover upgraded key, delete it.
125 * We either failed an update and must return to the old key,
126 * or we rebooted before commiting the keys in a freak accident.
127 * Either way, we can re-upgrade the key if we need to.
128 */
129 Keymaster keymaster;
130 if (pathExists(newKeyPath)) {
131 if (!android::base::ReadFileToString(newKeyPath, &sKey))
132 LOG(ERROR) << "Failed to read old key: " << dir;
133 else if (!keymaster.deleteKey(sKey))
134 LOG(ERROR) << "Old key deletion failed, continuing anyway: " << dir;
135 else
136 unlink(newKeyPath.c_str());
137 }
138 bool needs_cp = cp_needsCheckpoint();
139 if (!android::vold::retrieveKey(create_if_absent, dir, temp, key, needs_cp)) return false;
140 if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach();
Paul Crowleyd5759812016-06-02 11:04:27 -0700141 return true;
142}
143
Daniel Rosenberg690d6de2018-12-14 01:08:10 -0800144} // namespace vold
145} // namespace android
146
Pavel Grafove2e2d302017-08-01 17:15:53 +0100147static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) {
148 KeyBuffer hex_key;
Paul Crowleyd5759812016-06-02 11:04:27 -0700149 if (android::vold::StrToHex(key, hex_key) != android::OK) {
150 LOG(ERROR) << "Failed to turn key to hex";
Pavel Grafove2e2d302017-08-01 17:15:53 +0100151 return KeyBuffer();
Paul Crowleyd5759812016-06-02 11:04:27 -0700152 }
Pavel Grafove2e2d302017-08-01 17:15:53 +0100153 auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0";
Paul Crowleyd5759812016-06-02 11:04:27 -0700154 return res;
155}
156
Paul Crowley14c8c072018-09-18 13:30:21 -0700157static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200158 if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700159 PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
160 return false;
161 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700162 return true;
163}
164
Paul Crowley14c8c072018-09-18 13:30:21 -0700165static struct dm_ioctl* dm_ioctl_init(char* buffer, size_t buffer_size, const std::string& dm_name) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700166 if (buffer_size < sizeof(dm_ioctl)) {
167 LOG(ERROR) << "dm_ioctl buffer too small";
168 return nullptr;
169 }
170
171 memset(buffer, 0, buffer_size);
Paul Crowley14c8c072018-09-18 13:30:21 -0700172 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
Paul Crowleyd5759812016-06-02 11:04:27 -0700173 io->data_size = buffer_size;
174 io->data_start = sizeof(struct dm_ioctl);
175 io->version[0] = 4;
176 io->version[1] = 0;
177 io->version[2] = 0;
178 io->flags = 0;
179 dm_name.copy(io->name, sizeof(io->name));
180 return io;
181}
182
183static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec,
Pavel Grafove2e2d302017-08-01 17:15:53 +0100184 const std::string& target_type, const KeyBuffer& crypt_params,
Paul Crowleyd5759812016-06-02 11:04:27 -0700185 std::string* crypto_blkdev) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 android::base::unique_fd dm_fd(
187 TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC, 0)));
Paul Crowleye4c93da2017-06-16 09:21:18 -0700188 if (dm_fd == -1) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700189 PLOG(ERROR) << "Cannot open device-mapper";
190 return false;
191 }
192 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
193 auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
194 if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) {
195 PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name;
196 return false;
197 }
198
199 // Get the device status, in particular, the name of its device file
200 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
201 if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) {
202 PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name;
203 return false;
204 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700205 *crypto_blkdev = std::string() + "/dev/block/dm-" +
206 std::to_string((io->dev & 0xff) | ((io->dev >> 12) & 0xfff00));
Paul Crowleyd5759812016-06-02 11:04:27 -0700207
208 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
Pavel Grafove2e2d302017-08-01 17:15:53 +0100209 size_t paramix = io->data_start + sizeof(struct dm_target_spec);
210 size_t nullix = paramix + crypt_params.size();
Paul Crowley14c8c072018-09-18 13:30:21 -0700211 size_t endix = (nullix + 1 + 7) & 8; // Add room for \0 and align to 8 byte boundary
Paul Crowleyd5759812016-06-02 11:04:27 -0700212
213 if (endix > sizeof(buffer)) {
214 LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE";
215 return false;
216 }
217
218 io->target_count = 1;
Paul Crowley14c8c072018-09-18 13:30:21 -0700219 auto tgt = (struct dm_target_spec*)(buffer + io->data_start);
Paul Crowleyd5759812016-06-02 11:04:27 -0700220 tgt->status = 0;
221 tgt->sector_start = 0;
222 tgt->length = nr_sec;
223 target_type.copy(tgt->target_type, sizeof(tgt->target_type));
Pavel Grafove2e2d302017-08-01 17:15:53 +0100224 memcpy(buffer + paramix, crypt_params.data(),
Paul Crowley14c8c072018-09-18 13:30:21 -0700225 std::min(crypt_params.size(), sizeof(buffer) - paramix));
Paul Crowleyd5759812016-06-02 11:04:27 -0700226 buffer[nullix] = '\0';
227 tgt->next = endix;
228
Paul Crowley14c8c072018-09-18 13:30:21 -0700229 for (int i = 0;; i++) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700230 if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) {
231 break;
232 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700233 if (i + 1 >= TABLE_LOAD_RETRIES) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700234 PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed";
235 return false;
236 }
237 PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying";
238 usleep(500000);
239 }
240
241 // Resume this device to activate it
242 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
243 if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) {
244 PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name;
245 return false;
246 }
247 return true;
248}
249
Eric Biggersa701c452018-10-23 13:06:55 -0700250bool fscrypt_mount_metadata_encrypted(const std::string& mount_point, bool needs_encrypt) {
251 LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt;
Paul Crowley0fd26262018-01-30 09:48:19 -0800252 auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
253 if (encrypted_state != "") {
Eric Biggersa701c452018-10-23 13:06:55 -0700254 LOG(DEBUG) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state;
Paul Crowleyd5759812016-06-02 11:04:27 -0700255 return false;
256 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800257 auto data_rec = fs_mgr_get_entry_for_mount_point(fstab_default, mount_point);
Paul Crowleyd5759812016-06-02 11:04:27 -0700258 if (!data_rec) {
259 LOG(ERROR) << "Failed to get data_rec";
260 return false;
261 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800262 KeyBuffer key;
263 if (!read_key(data_rec, needs_encrypt, &key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700264 uint64_t nr_sec;
265 if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700266 std::string crypto_blkdev;
267 if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE,
Paul Crowley0fd26262018-01-30 09:48:19 -0800268 default_key_params(data_rec->blk_device, key), &crypto_blkdev))
Paul Crowleyd5759812016-06-02 11:04:27 -0700269 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800270 // FIXME handle the corrupt case
271 if (needs_encrypt) {
272 LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec;
273 off64_t size_already_done = 0;
274 auto rc =
275 cryptfs_enable_inplace(const_cast<char*>(crypto_blkdev.c_str()), data_rec->blk_device,
276 nr_sec, &size_already_done, nr_sec, 0, false);
277 if (rc != 0) {
278 LOG(ERROR) << "Inplace crypto failed with code: " << rc;
279 return false;
280 }
281 if (static_cast<uint64_t>(size_already_done) != nr_sec) {
282 LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done;
283 return false;
284 }
285 LOG(INFO) << "Inplace encryption complete";
Paul Crowleyd5759812016-06-02 11:04:27 -0700286 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700287
Paul Crowley0fd26262018-01-30 09:48:19 -0800288 LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
Paul Crowleyd5759812016-06-02 11:04:27 -0700289 mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str());
Paul Crowleyd5759812016-06-02 11:04:27 -0700290 return true;
291}