blob: 8e7d8f4399cd167f5cfe0911542ae3a5c7195c53 [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 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
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Shivaprasad Hongal92292622018-07-05 14:49:12 -070019#include "Keymaster.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000020#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080022#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070023#include "VoldUtil.h"
24
Paul Crowleya3630362016-05-17 14:17:56 -070025#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000026#include <map>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000027#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070028#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080029#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070030#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000031
Paul Crowleydf528a72016-03-09 09:31:37 -080032#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070035#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070036#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080037#include <sys/mount.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070040#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000041
Paul Crowley480fcd22015-08-24 14:53:28 +010042#include <private/android_filesystem_config.h>
43
Paul Crowley82b41ff2017-10-20 08:17:54 -070044#include "android/os/IVold.h"
45
Paul Lawrence731a7a22015-04-28 22:14:15 +000046#include "cryptfs.h"
47
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070048#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060049#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070050
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080051#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070052#include <cutils/properties.h>
53
Eric Biggersa701c452018-10-23 13:06:55 -070054#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070055#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080056
Elliott Hughes7e128fb2015-12-04 15:50:53 -080057#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080058#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070059#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080060#include <android-base/stringprintf.h>
Jieb6698d52018-11-12 15:26:02 +080061#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000062
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080063using android::base::StringPrintf;
Pavel Grafovb350ed02017-07-27 17:34:57 +010064using android::base::WriteStringToFile;
Tom Cherry4c5bde22019-01-29 14:34:01 -080065using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000066using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010067using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070068using android::vold::Keymaster;
69using android::hardware::keymaster::V4_0::KeyFormat;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080070
Paul Lawrence731a7a22015-04-28 22:14:15 +000071namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000072
Paul Crowley3aa914d2017-10-09 16:35:51 -070073struct PolicyKeyRef {
74 std::string contents_mode;
75 std::string filenames_mode;
76 std::string key_raw_ref;
77};
78
Eric Biggersa701c452018-10-23 13:06:55 -070079const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080080const std::string device_key_path = device_key_dir + "/key";
81const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080082
Paul Crowleydf528a72016-03-09 09:31:37 -080083const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
84const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070085const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000086
Paul Crowley26a53882017-10-26 11:16:39 -070087const std::string systemwide_volume_key_dir =
88 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
89
Paul Crowleydf528a72016-03-09 09:31:37 -080090bool s_global_de_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080091
Paul Crowleydf528a72016-03-09 09:31:37 -080092// Some users are ephemeral, don't try to wipe their keys from disk
93std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080094
Paul Crowleydf528a72016-03-09 09:31:37 -080095// Map user ids to key references
96std::map<userid_t, std::string> s_de_key_raw_refs;
97std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070098// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +010099std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000100
Paul Crowley14c8c072018-09-18 13:30:21 -0700101} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +0000102
Eric Biggersa701c452018-10-23 13:06:55 -0700103static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000104 return property_get_bool("persist.sys.emulate_fbe", false);
105}
106
Paul Crowley3b71fc52017-10-09 10:55:21 -0700107static const char* escape_empty(const std::string& value) {
108 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000109}
110
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000111static std::string get_de_key_path(userid_t user_id) {
112 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
113}
114
Paul Crowleya3630362016-05-17 14:17:56 -0700115static std::string get_ce_key_directory_path(userid_t user_id) {
116 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
117}
118
119// Returns the keys newest first
120static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
121 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
122 if (!dirp) {
123 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
124 return std::vector<std::string>();
125 }
126 std::vector<std::string> result;
127 for (;;) {
128 errno = 0;
129 auto const entry = readdir(dirp.get());
130 if (!entry) {
131 if (errno) {
132 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
133 return std::vector<std::string>();
134 }
135 break;
136 }
137 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
138 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
139 continue;
140 }
141 result.emplace_back(directory_path + "/" + entry->d_name);
142 }
143 std::sort(result.begin(), result.end());
144 std::reverse(result.begin(), result.end());
145 return result;
146}
147
148static std::string get_ce_key_current_path(const std::string& directory_path) {
149 return directory_path + "/current";
150}
151
152static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700153 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700154 if (paths.empty()) {
155 *ce_key_path = get_ce_key_current_path(directory_path);
156 return true;
157 }
158 for (unsigned int i = 0; i < UINT_MAX; i++) {
159 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
160 if (paths[0] < candidate) {
161 *ce_key_path = candidate;
162 return true;
163 }
164 }
165 return false;
166}
167
168// Discard all keys but the named one; rename it to canonical name.
169// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700170static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700171 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700172 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700173 if (other_path != to_fix) {
174 android::vold::destroyKey(other_path);
175 }
176 }
177 auto const current_path = get_ce_key_current_path(directory_path);
178 if (to_fix != current_path) {
179 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
180 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
181 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800182 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700183 }
184 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800185 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700186}
187
188static bool read_and_fixate_user_ce_key(userid_t user_id,
189 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700190 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700191 auto const directory_path = get_ce_key_directory_path(user_id);
192 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700193 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700194 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
195 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
196 LOG(DEBUG) << "Successfully retrieved key";
197 fixate_user_ce_key(directory_path, ce_key_path, paths);
198 return true;
199 }
200 }
201 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
202 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100203}
204
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700205bool is_wrapped_key_supported() {
Shivaprasad Hongal0fe25652018-09-15 17:36:19 -0700206 return fs_mgr_is_wrapped_key_supported(
207 fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT));
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700208}
209
210bool is_wrapped_key_supported_external() {
211 return false;
212}
213
Paul Crowleydf528a72016-03-09 09:31:37 -0800214static bool read_and_install_user_ce_key(userid_t user_id,
215 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000216 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100217 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700218 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000219 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700220
221 if (is_wrapped_key_supported()) {
222 KeyBuffer ephemeral_wrapped_key;
223 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
224 LOG(ERROR) << "Failed to export ce key";
225 return false;
226 }
227 ce_key = std::move(ephemeral_wrapped_key);
228 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700229 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100230 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000231 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000232 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000233 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000234}
235
Paul Crowleydf528a72016-03-09 09:31:37 -0800236static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000237 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000238 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
239 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000240 return false;
241 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000242 return true;
243}
244
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600245static bool destroy_dir(const std::string& dir) {
246 LOG(DEBUG) << "Destroying: " << dir;
247 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
248 PLOG(ERROR) << "Failed to destroy " << dir;
249 return false;
250 }
251 return true;
252}
253
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000254// NB this assumes that there is only one thread listening for crypt commands, because
255// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000256static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100257 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700258
259 if(is_wrapped_key_supported()) {
260 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
261 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
262 } else {
263 if (!android::vold::randomKey(&de_key)) return false;
264 if (!android::vold::randomKey(&ce_key)) return false;
265 }
266
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000267 if (create_ephemeral) {
268 // If the key should be created as ephemeral, don't store it.
269 s_ephemeral_users.insert(user_id);
270 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700271 auto const directory_path = get_ce_key_directory_path(user_id);
272 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
273 auto const paths = get_ce_key_paths(directory_path);
274 std::string ce_key_path;
275 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700276 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
277 ce_key))
278 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700279 fixate_user_ce_key(directory_path, ce_key_path, paths);
280 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700281 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700282 kEmptyAuthentication, de_key))
283 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100284 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700285
286 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000287 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000288 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700289
290 if (is_wrapped_key_supported()) {
291 KeyBuffer ephemeral_wrapped_de_key;
292 KeyBuffer ephemeral_wrapped_ce_key;
293
294 /* Export and install the DE keys */
295 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
296 LOG(ERROR) << "Failed to export de_key";
297 return false;
298 }
299 /* Export and install the CE keys */
300 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
301 LOG(ERROR) << "Failed to export de_key";
302 return false;
303 }
304
305 de_key = std::move(ephemeral_wrapped_de_key);
306 ce_key = std::move(ephemeral_wrapped_ce_key);
307 }
308 if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700309 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700310 s_ce_keys[user_id] = std::move(ce_key);
311
312 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000313 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700314
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000315 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000316 return true;
317}
318
Paul Crowleydf528a72016-03-09 09:31:37 -0800319static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
320 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000321 auto refi = key_map.find(user_id);
322 if (refi == key_map.end()) {
323 LOG(ERROR) << "Cannot find key for " << user_id;
324 return false;
325 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800326 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000327 return true;
328}
329
Paul Crowley3aa914d2017-10-09 16:35:51 -0700330static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800331 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
332 if (entry == nullptr) {
333 return;
334 }
335 key_ref->contents_mode = entry->file_contents_mode;
336 key_ref->filenames_mode = entry->file_names_mode;
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700337}
338
Paul Crowley3aa914d2017-10-09 16:35:51 -0700339static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700340 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700341 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
342 key_ref.filenames_mode.c_str()) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100343}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100344
Paul Crowleydf528a72016-03-09 09:31:37 -0800345static bool is_numeric(const char* name) {
346 for (const char* p = name; *p != '\0'; p++) {
347 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000348 }
349 return true;
350}
351
352static bool load_all_de_keys() {
353 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800354 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000355 if (!dirp) {
356 PLOG(ERROR) << "Unable to read de key directory";
357 return false;
358 }
359 for (;;) {
360 errno = 0;
361 auto entry = readdir(dirp.get());
362 if (!entry) {
363 if (errno) {
364 PLOG(ERROR) << "Unable to read de key directory";
365 return false;
366 }
367 break;
368 }
369 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
370 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
371 continue;
372 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600373 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000374 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000375 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100376 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800377 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000378 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700379 if (is_wrapped_key_supported()) {
380 KeyBuffer ephemeral_wrapped_key;
381 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
382 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
383 return false;
384 }
385 key = std::move(ephemeral_wrapped_key);
386 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700387 if (!android::vold::installKey(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000388 s_de_key_raw_refs[user_id] = raw_ref;
389 LOG(DEBUG) << "Installed de key for user " << user_id;
390 }
391 }
Eric Biggersa701c452018-10-23 13:06:55 -0700392 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000393 // correct policy set on them, and that no rogue ones exist.
394 return true;
395}
396
Eric Biggersa701c452018-10-23 13:06:55 -0700397bool fscrypt_initialize_global_de() {
398 LOG(INFO) << "fscrypt_initialize_global_de";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700399 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800400
Paul Crowley38132a12016-02-09 09:50:32 +0000401 if (s_global_de_initialized) {
402 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000403 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800404 }
405
Paul Crowley3aa914d2017-10-09 16:35:51 -0700406 PolicyKeyRef device_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700407 wrapped_key_supported = is_wrapped_key_supported();
408
409 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication,
410 device_key_path, device_key_temp,
411 &device_ref.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700412 return false;
413 get_data_file_encryption_modes(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800414
Paul Crowley3aa914d2017-10-09 16:35:51 -0700415 std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
Eric Biggersa701c452018-10-23 13:06:55 -0700416 std::string mode_filename = std::string("/data") + fscrypt_key_mode;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800417 if (!android::base::WriteStringToFile(modestring, mode_filename)) {
Paul Lawrence6e410592016-05-24 14:20:38 -0700418 PLOG(ERROR) << "Cannot save type";
419 return false;
420 }
421
Eric Biggersa701c452018-10-23 13:06:55 -0700422 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700423 if (!android::base::WriteStringToFile(device_ref.key_raw_ref, ref_filename)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700424 PLOG(ERROR) << "Cannot save key reference to:" << ref_filename;
Paul Crowley76107cb2016-02-09 10:04:39 +0000425 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800426 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700427 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800428
Paul Crowley38132a12016-02-09 09:50:32 +0000429 s_global_de_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000430 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800431}
432
Eric Biggersa701c452018-10-23 13:06:55 -0700433bool fscrypt_init_user0() {
434 LOG(DEBUG) << "fscrypt_init_user0";
435 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000436 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
437 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
438 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700439 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000440 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000441 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700442 // TODO: switch to loading only DE_0 here once framework makes
443 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000444 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000445 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700446 // We can only safely prepare DE storage here, since CE keys are probably
447 // entangled with user credentials. The framework will always prepare CE
448 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700449 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700450 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000451 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700452 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700453
454 // If this is a non-FBE device that recently left an emulated mode,
455 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700456 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
457 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700458 }
459
Paul Crowley76107cb2016-02-09 10:04:39 +0000460 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000461}
462
Eric Biggersa701c452018-10-23 13:06:55 -0700463bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
464 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
465 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000466 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000467 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000468 // FIXME test for existence of key that is not loaded yet
469 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700470 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800471 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000472 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000473 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800474 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000475 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000476 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000477 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000478 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800479}
480
Pavel Grafovb350ed02017-07-27 17:34:57 +0100481static void drop_caches() {
482 // Clean any dirty pages (otherwise they won't be dropped).
483 sync();
484 // Drop inode and page caches.
485 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
486 PLOG(ERROR) << "Failed to drop caches during key eviction";
487 }
488}
489
Andrew Scull7ec25c72016-10-31 10:28:25 +0000490static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000491 bool success = true;
492 std::string raw_ref;
493 // If we haven't loaded the CE key, no need to evict it.
494 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700495 success &= android::vold::evictKey(raw_ref);
Pavel Grafovb350ed02017-07-27 17:34:57 +0100496 drop_caches();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000497 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700498
499 if(is_wrapped_key_supported()) {
500 KeyBuffer key;
501 key = s_ce_keys[user_id];
502
503 std::string keystr(key.data(), key.size());
504 Keymaster keymaster;
505
506 if (!keymaster) {
507 s_ce_keys.erase(user_id);
508 s_ce_key_raw_refs.erase(user_id);
509 return false;
510 }
511 keymaster.deleteKey(keystr);
512 }
513
514 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000515 s_ce_key_raw_refs.erase(user_id);
516 return success;
517}
518
Eric Biggersa701c452018-10-23 13:06:55 -0700519bool fscrypt_destroy_user_key(userid_t user_id) {
520 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
521 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000522 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000523 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000524 bool success = true;
525 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000526 success &= evict_ce_key(user_id);
Paul Crowley14c8c072018-09-18 13:30:21 -0700527 success &=
528 lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000529 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000530 auto it = s_ephemeral_users.find(user_id);
531 if (it != s_ephemeral_users.end()) {
532 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000533 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700534 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700535 success &= android::vold::destroyKey(path);
536 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700537 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700538 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700539 success &= android::vold::destroyKey(de_key_path);
540 } else {
541 LOG(INFO) << "Not present so not erasing: " << de_key_path;
542 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100543 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000544 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100545}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800546
Paul Crowley76107cb2016-02-09 10:04:39 +0000547static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700548 if (chmod(path.c_str(), 0000) != 0) {
549 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000550 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700551 }
552#if EMULATED_USES_SELINUX
553 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
554 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000555 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700556 }
557#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000558 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700559}
560
Paul Crowley76107cb2016-02-09 10:04:39 +0000561static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700562 if (chmod(path.c_str(), mode) != 0) {
563 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000564 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700565 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700566 }
567#if EMULATED_USES_SELINUX
568 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
569 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000570 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700571 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700572 }
573#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000574 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700575}
576
Paul Crowley3b71fc52017-10-09 10:55:21 -0700577static bool parse_hex(const std::string& hex, std::string* result) {
578 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800579 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000580 return true;
581 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800582 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800583 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000584 return false;
585 }
586 return true;
587}
588
Paul Crowley3aa914d2017-10-09 16:35:51 -0700589static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
590 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
591}
592
Paul Crowley26a53882017-10-26 11:16:39 -0700593static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
594 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
595}
596
Paul Crowley3aa914d2017-10-09 16:35:51 -0700597static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
598 PolicyKeyRef* key_ref) {
Paul Crowley26a53882017-10-26 11:16:39 -0700599 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
600 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700601 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700602 if (android::vold::pathExists(secdiscardable_path)) {
603 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
604 return false;
605 } else {
606 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
607 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
608 return false;
609 }
610 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
611 return false;
612 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700613 auto key_path = volkey_path(misc_path, volume_uuid);
614 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
615 PLOG(ERROR) << "Creating directories for: " << key_path;
616 return false;
617 }
Paul Crowley26a53882017-10-26 11:16:39 -0700618 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700619 wrapped_key_supported = is_wrapped_key_supported_external();
620
Paul Crowley26a53882017-10-26 11:16:39 -0700621 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700622 &key_ref->key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700623 return false;
624 key_ref->contents_mode =
625 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
626 key_ref->filenames_mode =
627 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
628 return true;
629}
630
631static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700632 auto path = volkey_path(misc_path, volume_uuid);
633 if (!android::vold::pathExists(path)) return true;
634 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700635}
636
Eric Biggersa701c452018-10-23 13:06:55 -0700637bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700638 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700639 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700640 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700641 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000642 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700643 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800644 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700645 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700646 auto auth =
647 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700648 auto const directory_path = get_ce_key_directory_path(user_id);
649 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700650
651 KeyBuffer ce_key;
652 if(is_wrapped_key_supported()) {
653 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
654 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
655 LOG(DEBUG) << "Successfully retrieved key";
656 } else {
657 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
658 LOG(DEBUG) << "Successfully retrieved key";
659 }
660 }
661 } else {
662 auto it = s_ce_keys.find(user_id);
663 if (it == s_ce_keys.end()) {
664 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
665 return false;
666 }
667 ce_key = it->second;
668 }
669
Paul Crowleya3630362016-05-17 14:17:56 -0700670 std::string ce_key_path;
671 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700672 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800673 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700674 return true;
675}
676
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800677bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700678 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800679 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700680 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800681 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700682 if (s_ephemeral_users.count(user_id) != 0) return true;
683 std::string token, secret;
684
685 if (!parse_hex(token_hex, &token)) return false;
686 if (!parse_hex(secret_hex, &secret)) return false;
687
688 if (is_wrapped_key_supported()) {
689 auto const directory_path = get_ce_key_directory_path(user_id);
690 auto const paths = get_ce_key_paths(directory_path);
691
692 KeyBuffer ce_key;
693 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
694
695 auto auth = android::vold::KeyAuthentication(token, secret);
696 /* Retrieve key while removing a pin. A secret is needed */
697 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
698 LOG(DEBUG) << "Successfully retrieved key";
699 } else {
700 /* Retrieve key when going None to swipe and vice versa when a
701 synthetic password is present */
702 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
703 LOG(DEBUG) << "Successfully retrieved key";
704 }
705 }
706
707 std::string ce_key_path;
708 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
709 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
710 return false;
711 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800712 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700713 }
714 return true;
715}
716
Eric Biggersa701c452018-10-23 13:06:55 -0700717bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
718 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
719 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700720 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700721 auto const directory_path = get_ce_key_directory_path(user_id);
722 auto const paths = get_ce_key_paths(directory_path);
723 if (paths.empty()) {
724 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
725 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000726 }
Paul Crowleya3630362016-05-17 14:17:56 -0700727 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000728 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000729}
730
Jeff Sharkey47695b22016-02-01 17:02:29 -0700731// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700732bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700733 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700734 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700735 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700736 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000737 if (s_ce_key_raw_refs.count(user_id) != 0) {
738 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000739 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000740 }
741 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800742 if (!parse_hex(token_hex, &token)) return false;
743 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000744 android::vold::KeyAuthentication auth(token, secret);
745 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000746 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000747 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800748 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700749 } else {
750 // When in emulation mode, we just use chmod. However, we also
751 // unlock directories when not in emulation mode, to bring devices
752 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000753 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800754 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700755 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
756 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700757 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000758 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700759 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800760 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000761 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800762}
763
Jeff Sharkey47695b22016-02-01 17:02:29 -0700764// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700765bool fscrypt_lock_user_key(userid_t user_id) {
766 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
767 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000768 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700769 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800770 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000771 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800772 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700773 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
774 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000775 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000776 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800777 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800778 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700779
Paul Crowley76107cb2016-02-09 10:04:39 +0000780 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800781}
782
Paul Crowley82b41ff2017-10-20 08:17:54 -0700783static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
784 userid_t user_id, int flags) {
785 if (0 != android::vold::ForkExecvp(
786 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
787 std::to_string(user_id), std::to_string(flags)})) {
788 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700789 return false;
790 }
791 return true;
792}
793
Eric Biggersa701c452018-10-23 13:06:55 -0700794bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700795 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700796 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800797 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700798
Paul Crowley82b41ff2017-10-20 08:17:54 -0700799 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600800 // DE_sys key
801 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
802 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
803 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600804
805 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700806 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
807 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800808 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700809 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
810
Paul Crowley3b71fc52017-10-09 10:55:21 -0700811 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700812 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600813#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700814 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700815 multiuser_get_uid(user_id, AID_EVERYBODY)))
816 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600817#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700818 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600819
Paul Crowley6b756ce2017-10-05 14:07:09 -0700820 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
821 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800822 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700823 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000824 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000825
Eric Biggersa701c452018-10-23 13:06:55 -0700826 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700827 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700828 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700829 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
830 get_data_file_encryption_modes(&de_ref);
831 if (!ensure_policy(de_ref, system_de_path)) return false;
832 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800833 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700834 } else {
835 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700836 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700837 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700838 }
Paul Crowley285956f2016-01-20 13:12:38 +0000839 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800840
Paul Crowley82b41ff2017-10-20 08:17:54 -0700841 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600842 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700843 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
844 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800845 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600846 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
847 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800848
Paul Crowley3b71fc52017-10-09 10:55:21 -0700849 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700850 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
851 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800852 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700853 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000854 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
855 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700856
Eric Biggersa701c452018-10-23 13:06:55 -0700857 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700858 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700859 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700860 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
861 get_data_file_encryption_modes(&ce_ref);
862 if (!ensure_policy(ce_ref, system_ce_path)) return false;
863 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800864 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700865
Paul Crowley3aa914d2017-10-09 16:35:51 -0700866 } else {
867 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700868 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700869 if (!ensure_policy(ce_ref, media_ce_path)) return false;
870 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700871 }
Paul Crowley1a965262017-10-13 13:49:50 -0700872
873 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700874 // Now that credentials have been installed, we can run restorecon
875 // over these paths
876 // NOTE: these paths need to be kept in sync with libselinux
877 android::vold::RestoreconRecursive(system_ce_path);
878 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700879 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800880 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700881 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800882
Paul Crowley76107cb2016-02-09 10:04:39 +0000883 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800884}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600885
Eric Biggersa701c452018-10-23 13:06:55 -0700886bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
887 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600888 << ", user " << user_id << ", flags " << flags;
889 bool res = true;
890
Paul Crowley82b41ff2017-10-20 08:17:54 -0700891 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
892
893 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700894 // CE_n key
895 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
896 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800897 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700898 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
899 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
900
901 res &= destroy_dir(media_ce_path);
902 res &= destroy_dir(user_ce_path);
903 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700904 res &= destroy_dir(system_ce_path);
905 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800906 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700907 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700908 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700909 res &= destroy_volkey(misc_ce_path, volume_uuid);
910 }
Paul Crowley8e550662017-10-16 17:01:44 -0700911 }
912 }
913
Paul Crowley82b41ff2017-10-20 08:17:54 -0700914 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600915 // DE_sys key
916 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
917 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
918 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600919
920 // DE_n key
921 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
922 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800923 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600924 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
925
Paul Crowley8e550662017-10-16 17:01:44 -0700926 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700927 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600928 res &= destroy_dir(system_legacy_path);
929#if MANAGE_MISC_DIRS
930 res &= destroy_dir(misc_legacy_path);
931#endif
932 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600933 res &= destroy_dir(system_de_path);
934 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800935 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700936 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700937 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700938 res &= destroy_volkey(misc_de_path, volume_uuid);
939 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600940 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600941 }
942
943 return res;
944}
Rubin Xu2436e272017-04-27 20:43:10 +0100945
Paul Crowleyc6433a22017-10-24 14:54:43 -0700946static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
947 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
948 if (!dirp) {
949 PLOG(ERROR) << "Unable to open directory: " + directory_path;
950 return false;
951 }
952 bool res = true;
953 for (;;) {
954 errno = 0;
955 auto const entry = readdir(dirp.get());
956 if (!entry) {
957 if (errno) {
958 PLOG(ERROR) << "Unable to read directory: " + directory_path;
959 return false;
960 }
961 break;
962 }
963 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
964 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
965 continue;
966 }
967 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
968 }
969 return res;
970}
971
Eric Biggersa701c452018-10-23 13:06:55 -0700972bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700973 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700974 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700975 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
976 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700977 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
978 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
979 return res;
980}