blob: e1564244f02d36fe9738a16b1be9d7dab7ba1eb0 [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
33#include <android-base/logging.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080034#include <android-base/properties.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070035#include <android-base/unique_fd.h>
Paul Crowley0fd26262018-01-30 09:48:19 -080036#include <cutils/fs.h>
Paul Crowleyd5759812016-06-02 11:04:27 -070037#include <fs_mgr.h>
38
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070039#include "Checkpoint.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070040#include "EncryptInplace.h"
41#include "KeyStorage.h"
42#include "KeyUtil.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070043#include "Utils.h"
44#include "VoldUtil.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070045#include "secontext.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070046
Paul Crowleyd5759812016-06-02 11:04:27 -070047#define DM_CRYPT_BUF_SIZE 4096
48#define TABLE_LOAD_RETRIES 10
49#define DEFAULT_KEY_TARGET_TYPE "default-key"
50
Pavel Grafove2e2d302017-08-01 17:15:53 +010051using android::vold::KeyBuffer;
52
Paul Crowleyd5759812016-06-02 11:04:27 -070053static const std::string kDmNameUserdata = "userdata";
54
55static 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 Crowleye2ee1522017-09-26 14:05:26 -070062 auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070063 const_cast<char*>(blk_device), nullptr,
64 android::vold::cp_needsCheckpoint());
Paul Crowleyd5759812016-06-02 11:04:27 -070065 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 Crowley0fd26262018-01-30 09:48:19 -080077static bool read_key(struct fstab_rec const* data_rec, bool create_if_absent, KeyBuffer* key) {
Paul Crowleyd5759812016-06-02 11:04:27 -070078 if (!data_rec->key_dir) {
79 LOG(ERROR) << "Failed to get key_dir";
80 return false;
81 }
Paul Crowleyd5759812016-06-02 11:04:27 -070082 std::string key_dir = data_rec->key_dir;
83 auto dir = key_dir + "/key";
Paul Crowley98a23a12018-05-09 13:01:16 -070084 LOG(DEBUG) << "key_dir/key: " << dir;
85 if (fs_mkdirs(dir.c_str(), 0700)) {
Paul Crowley0fd26262018-01-30 09:48:19 -080086 PLOG(ERROR) << "Creating directories: " << dir;
Paul Crowley98a23a12018-05-09 13:01:16 -070087 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -080088 }
Paul Crowleyd5759812016-06-02 11:04:27 -070089 auto temp = key_dir + "/tmp";
90 if (!android::vold::retrieveKey(create_if_absent, dir, temp, key)) return false;
91 return true;
92}
93
Pavel Grafove2e2d302017-08-01 17:15:53 +010094static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) {
95 KeyBuffer hex_key;
Paul Crowleyd5759812016-06-02 11:04:27 -070096 if (android::vold::StrToHex(key, hex_key) != android::OK) {
97 LOG(ERROR) << "Failed to turn key to hex";
Pavel Grafove2e2d302017-08-01 17:15:53 +010098 return KeyBuffer();
Paul Crowleyd5759812016-06-02 11:04:27 -070099 }
Pavel Grafove2e2d302017-08-01 17:15:53 +0100100 auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0";
Paul Crowleyd5759812016-06-02 11:04:27 -0700101 return res;
102}
103
Paul Crowley14c8c072018-09-18 13:30:21 -0700104static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200105 if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700106 PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
107 return false;
108 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700109 return true;
110}
111
Paul Crowley14c8c072018-09-18 13:30:21 -0700112static struct dm_ioctl* dm_ioctl_init(char* buffer, size_t buffer_size, const std::string& dm_name) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700113 if (buffer_size < sizeof(dm_ioctl)) {
114 LOG(ERROR) << "dm_ioctl buffer too small";
115 return nullptr;
116 }
117
118 memset(buffer, 0, buffer_size);
Paul Crowley14c8c072018-09-18 13:30:21 -0700119 struct dm_ioctl* io = (struct dm_ioctl*)buffer;
Paul Crowleyd5759812016-06-02 11:04:27 -0700120 io->data_size = buffer_size;
121 io->data_start = sizeof(struct dm_ioctl);
122 io->version[0] = 4;
123 io->version[1] = 0;
124 io->version[2] = 0;
125 io->flags = 0;
126 dm_name.copy(io->name, sizeof(io->name));
127 return io;
128}
129
130static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec,
Pavel Grafove2e2d302017-08-01 17:15:53 +0100131 const std::string& target_type, const KeyBuffer& crypt_params,
Paul Crowleyd5759812016-06-02 11:04:27 -0700132 std::string* crypto_blkdev) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700133 android::base::unique_fd dm_fd(
134 TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC, 0)));
Paul Crowleye4c93da2017-06-16 09:21:18 -0700135 if (dm_fd == -1) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700136 PLOG(ERROR) << "Cannot open device-mapper";
137 return false;
138 }
139 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
140 auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
141 if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) {
142 PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name;
143 return false;
144 }
145
146 // Get the device status, in particular, the name of its device file
147 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
148 if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) {
149 PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name;
150 return false;
151 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700152 *crypto_blkdev = std::string() + "/dev/block/dm-" +
153 std::to_string((io->dev & 0xff) | ((io->dev >> 12) & 0xfff00));
Paul Crowleyd5759812016-06-02 11:04:27 -0700154
155 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
Pavel Grafove2e2d302017-08-01 17:15:53 +0100156 size_t paramix = io->data_start + sizeof(struct dm_target_spec);
157 size_t nullix = paramix + crypt_params.size();
Paul Crowley14c8c072018-09-18 13:30:21 -0700158 size_t endix = (nullix + 1 + 7) & 8; // Add room for \0 and align to 8 byte boundary
Paul Crowleyd5759812016-06-02 11:04:27 -0700159
160 if (endix > sizeof(buffer)) {
161 LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE";
162 return false;
163 }
164
165 io->target_count = 1;
Paul Crowley14c8c072018-09-18 13:30:21 -0700166 auto tgt = (struct dm_target_spec*)(buffer + io->data_start);
Paul Crowleyd5759812016-06-02 11:04:27 -0700167 tgt->status = 0;
168 tgt->sector_start = 0;
169 tgt->length = nr_sec;
170 target_type.copy(tgt->target_type, sizeof(tgt->target_type));
Pavel Grafove2e2d302017-08-01 17:15:53 +0100171 memcpy(buffer + paramix, crypt_params.data(),
Paul Crowley14c8c072018-09-18 13:30:21 -0700172 std::min(crypt_params.size(), sizeof(buffer) - paramix));
Paul Crowleyd5759812016-06-02 11:04:27 -0700173 buffer[nullix] = '\0';
174 tgt->next = endix;
175
Paul Crowley14c8c072018-09-18 13:30:21 -0700176 for (int i = 0;; i++) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700177 if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) {
178 break;
179 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700180 if (i + 1 >= TABLE_LOAD_RETRIES) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700181 PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed";
182 return false;
183 }
184 PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying";
185 usleep(500000);
186 }
187
188 // Resume this device to activate it
189 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
190 if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) {
191 PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name;
192 return false;
193 }
194 return true;
195}
196
Eric Biggersa701c452018-10-23 13:06:55 -0700197bool fscrypt_mount_metadata_encrypted(const std::string& mount_point, bool needs_encrypt) {
198 LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt;
Paul Crowley0fd26262018-01-30 09:48:19 -0800199 auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
200 if (encrypted_state != "") {
Eric Biggersa701c452018-10-23 13:06:55 -0700201 LOG(DEBUG) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state;
Paul Crowleyd5759812016-06-02 11:04:27 -0700202 return false;
203 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800204 auto data_rec = fs_mgr_get_entry_for_mount_point(fstab_default, mount_point);
Paul Crowleyd5759812016-06-02 11:04:27 -0700205 if (!data_rec) {
206 LOG(ERROR) << "Failed to get data_rec";
207 return false;
208 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800209 KeyBuffer key;
210 if (!read_key(data_rec, needs_encrypt, &key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700211 uint64_t nr_sec;
212 if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700213 std::string crypto_blkdev;
214 if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE,
Paul Crowley0fd26262018-01-30 09:48:19 -0800215 default_key_params(data_rec->blk_device, key), &crypto_blkdev))
Paul Crowleyd5759812016-06-02 11:04:27 -0700216 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800217 // FIXME handle the corrupt case
218 if (needs_encrypt) {
219 LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec;
220 off64_t size_already_done = 0;
221 auto rc =
222 cryptfs_enable_inplace(const_cast<char*>(crypto_blkdev.c_str()), data_rec->blk_device,
223 nr_sec, &size_already_done, nr_sec, 0, false);
224 if (rc != 0) {
225 LOG(ERROR) << "Inplace crypto failed with code: " << rc;
226 return false;
227 }
228 if (static_cast<uint64_t>(size_already_done) != nr_sec) {
229 LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done;
230 return false;
231 }
232 LOG(INFO) << "Inplace encryption complete";
Paul Crowleyd5759812016-06-02 11:04:27 -0700233 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700234
Paul Crowley0fd26262018-01-30 09:48:19 -0800235 LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
Paul Crowleyd5759812016-06-02 11:04:27 -0700236 mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str());
Paul Crowleyd5759812016-06-02 11:04:27 -0700237 return true;
238}