blob: 4a847e3dfa14bd27a94bfa91afd01b90d2afc616 [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010017#include "KeyBuffer.h"
Paul Crowleyd5759812016-06-02 11:04:27 -070018#include "MetadataCrypt.h"
19
20#include <string>
21#include <thread>
22#include <vector>
Pavel Grafove2e2d302017-08-01 17:15:53 +010023#include <algorithm>
Paul Crowleyd5759812016-06-02 11:04:27 -070024
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
Paul Crowleyd5759812016-06-02 11:04:27 -070039#include "EncryptInplace.h"
40#include "KeyStorage.h"
41#include "KeyUtil.h"
42#include "secontext.h"
43#include "Utils.h"
44#include "VoldUtil.h"
45
Paul Crowleyd5759812016-06-02 11:04:27 -070046#define DM_CRYPT_BUF_SIZE 4096
47#define TABLE_LOAD_RETRIES 10
48#define DEFAULT_KEY_TARGET_TYPE "default-key"
49
Pavel Grafove2e2d302017-08-01 17:15:53 +010050using android::vold::KeyBuffer;
51
Paul Crowleyd5759812016-06-02 11:04:27 -070052static const std::string kDmNameUserdata = "userdata";
53
54static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
55 // fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
56 // partitions in the fsck domain.
57 if (setexeccon(secontextFsck())) {
58 PLOG(ERROR) << "Failed to setexeccon";
59 return false;
60 }
Paul Crowleye2ee1522017-09-26 14:05:26 -070061 auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
Paul Crowleyd5759812016-06-02 11:04:27 -070062 const_cast<char*>(blk_device), nullptr);
63 if (setexeccon(nullptr)) {
64 PLOG(ERROR) << "Failed to clear setexeccon";
65 return false;
66 }
67 if (mount_rc != 0) {
68 LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc;
69 return false;
70 }
71 LOG(DEBUG) << "Mounted " << mount_point;
72 return true;
73}
74
Paul Crowley0fd26262018-01-30 09:48:19 -080075static bool read_key(struct fstab_rec const* data_rec, bool create_if_absent, KeyBuffer* key) {
Paul Crowleyd5759812016-06-02 11:04:27 -070076 if (!data_rec->key_dir) {
77 LOG(ERROR) << "Failed to get key_dir";
78 return false;
79 }
Paul Crowleyd5759812016-06-02 11:04:27 -070080 std::string key_dir = data_rec->key_dir;
81 auto dir = key_dir + "/key";
Paul Crowley0fd26262018-01-30 09:48:19 -080082 LOG(DEBUG) << "key_dir/key: " << key;
83 if (!fs_mkdirs(dir.c_str(), 0700)) {
84 PLOG(ERROR) << "Creating directories: " << dir;
85 }
Paul Crowleyd5759812016-06-02 11:04:27 -070086 auto temp = key_dir + "/tmp";
87 if (!android::vold::retrieveKey(create_if_absent, dir, temp, key)) return false;
88 return true;
89}
90
Pavel Grafove2e2d302017-08-01 17:15:53 +010091static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) {
92 KeyBuffer hex_key;
Paul Crowleyd5759812016-06-02 11:04:27 -070093 if (android::vold::StrToHex(key, hex_key) != android::OK) {
94 LOG(ERROR) << "Failed to turn key to hex";
Pavel Grafove2e2d302017-08-01 17:15:53 +010095 return KeyBuffer();
Paul Crowleyd5759812016-06-02 11:04:27 -070096 }
Pavel Grafove2e2d302017-08-01 17:15:53 +010097 auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0";
Paul Crowleyd5759812016-06-02 11:04:27 -070098 return res;
99}
100
101static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t *nr_sec) {
Paul Crowleye4c93da2017-06-16 09:21:18 -0700102 android::base::unique_fd dev_fd(TEMP_FAILURE_RETRY(open(
103 real_blkdev.c_str(), O_RDONLY | O_CLOEXEC, 0)));
104 if (dev_fd == -1) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700105 PLOG(ERROR) << "Unable to open " << real_blkdev << " to measure size";
106 return false;
107 }
108 unsigned long res;
109 // TODO: should use BLKGETSIZE64
110 get_blkdev_size(dev_fd.get(), &res);
111 if (res == 0) {
112 PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
113 return false;
114 }
115 *nr_sec = res;
116 return true;
117}
118
119static struct dm_ioctl* dm_ioctl_init(char *buffer, size_t buffer_size,
120 const std::string& dm_name) {
121 if (buffer_size < sizeof(dm_ioctl)) {
122 LOG(ERROR) << "dm_ioctl buffer too small";
123 return nullptr;
124 }
125
126 memset(buffer, 0, buffer_size);
127 struct dm_ioctl* io = (struct dm_ioctl*) buffer;
128 io->data_size = buffer_size;
129 io->data_start = sizeof(struct dm_ioctl);
130 io->version[0] = 4;
131 io->version[1] = 0;
132 io->version[2] = 0;
133 io->flags = 0;
134 dm_name.copy(io->name, sizeof(io->name));
135 return io;
136}
137
138static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec,
Pavel Grafove2e2d302017-08-01 17:15:53 +0100139 const std::string& target_type, const KeyBuffer& crypt_params,
Paul Crowleyd5759812016-06-02 11:04:27 -0700140 std::string* crypto_blkdev) {
Paul Crowleye4c93da2017-06-16 09:21:18 -0700141 android::base::unique_fd dm_fd(TEMP_FAILURE_RETRY(open(
142 "/dev/device-mapper", O_RDWR | O_CLOEXEC, 0)));
143 if (dm_fd == -1) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700144 PLOG(ERROR) << "Cannot open device-mapper";
145 return false;
146 }
147 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
148 auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
149 if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) {
150 PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name;
151 return false;
152 }
153
154 // Get the device status, in particular, the name of its device file
155 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
156 if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) {
157 PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name;
158 return false;
159 }
160 *crypto_blkdev = std::string() + "/dev/block/dm-" + std::to_string(
161 (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00));
162
163 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
Pavel Grafove2e2d302017-08-01 17:15:53 +0100164 size_t paramix = io->data_start + sizeof(struct dm_target_spec);
165 size_t nullix = paramix + crypt_params.size();
166 size_t endix = (nullix + 1 + 7) & 8; // Add room for \0 and align to 8 byte boundary
Paul Crowleyd5759812016-06-02 11:04:27 -0700167
168 if (endix > sizeof(buffer)) {
169 LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE";
170 return false;
171 }
172
173 io->target_count = 1;
174 auto tgt = (struct dm_target_spec *) (buffer + io->data_start);
175 tgt->status = 0;
176 tgt->sector_start = 0;
177 tgt->length = nr_sec;
178 target_type.copy(tgt->target_type, sizeof(tgt->target_type));
Pavel Grafove2e2d302017-08-01 17:15:53 +0100179 memcpy(buffer + paramix, crypt_params.data(),
180 std::min(crypt_params.size(), sizeof(buffer) - paramix));
Paul Crowleyd5759812016-06-02 11:04:27 -0700181 buffer[nullix] = '\0';
182 tgt->next = endix;
183
184 for (int i = 0; ; i++) {
185 if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) {
186 break;
187 }
188 if (i+1 >= TABLE_LOAD_RETRIES) {
189 PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed";
190 return false;
191 }
192 PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying";
193 usleep(500000);
194 }
195
196 // Resume this device to activate it
197 io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
198 if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) {
199 PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name;
200 return false;
201 }
202 return true;
203}
204
Paul Crowley0fd26262018-01-30 09:48:19 -0800205bool e4crypt_mount_metadata_encrypted(const std::string& mount_point, bool needs_encrypt) {
206 LOG(DEBUG) << "e4crypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt;
207 auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
208 if (encrypted_state != "") {
Paul Crowleyd5759812016-06-02 11:04:27 -0700209 LOG(DEBUG) << "e4crypt_enable_crypto got unexpected starting state: " << encrypted_state;
210 return false;
211 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800212 auto data_rec = fs_mgr_get_entry_for_mount_point(fstab_default, mount_point);
Paul Crowleyd5759812016-06-02 11:04:27 -0700213 if (!data_rec) {
214 LOG(ERROR) << "Failed to get data_rec";
215 return false;
216 }
Paul Crowley0fd26262018-01-30 09:48:19 -0800217 KeyBuffer key;
218 if (!read_key(data_rec, needs_encrypt, &key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700219 uint64_t nr_sec;
220 if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700221 std::string crypto_blkdev;
222 if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE,
Paul Crowley0fd26262018-01-30 09:48:19 -0800223 default_key_params(data_rec->blk_device, key), &crypto_blkdev))
Paul Crowleyd5759812016-06-02 11:04:27 -0700224 return false;
Paul Crowley0fd26262018-01-30 09:48:19 -0800225 // FIXME handle the corrupt case
226 if (needs_encrypt) {
227 LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec;
228 off64_t size_already_done = 0;
229 auto rc =
230 cryptfs_enable_inplace(const_cast<char*>(crypto_blkdev.c_str()), data_rec->blk_device,
231 nr_sec, &size_already_done, nr_sec, 0, false);
232 if (rc != 0) {
233 LOG(ERROR) << "Inplace crypto failed with code: " << rc;
234 return false;
235 }
236 if (static_cast<uint64_t>(size_already_done) != nr_sec) {
237 LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done;
238 return false;
239 }
240 LOG(INFO) << "Inplace encryption complete";
Paul Crowleyd5759812016-06-02 11:04:27 -0700241 }
Paul Crowleyd5759812016-06-02 11:04:27 -0700242
Paul Crowley0fd26262018-01-30 09:48:19 -0800243 LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
Paul Crowleyd5759812016-06-02 11:04:27 -0700244 mount_via_fs_mgr(data_rec->mount_point, crypto_blkdev.c_str());
Paul Crowleyd5759812016-06-02 11:04:27 -0700245 return true;
246}