blob: 6d510a09a3ce77e4041165fbd9ad887ed2edc3e1 [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() {
Yifan Hong804afe12019-02-07 12:56:47 -0800206 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700207}
208
209bool is_wrapped_key_supported_external() {
210 return false;
211}
212
Paul Crowleydf528a72016-03-09 09:31:37 -0800213static bool read_and_install_user_ce_key(userid_t user_id,
214 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000215 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100216 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700217 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000218 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700219
220 if (is_wrapped_key_supported()) {
221 KeyBuffer ephemeral_wrapped_key;
222 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
223 LOG(ERROR) << "Failed to export ce key";
224 return false;
225 }
226 ce_key = std::move(ephemeral_wrapped_key);
227 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700228 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100229 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000230 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000231 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000232 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000233}
234
Paul Crowleydf528a72016-03-09 09:31:37 -0800235static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000236 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000237 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
238 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000239 return false;
240 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000241 return true;
242}
243
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600244static bool destroy_dir(const std::string& dir) {
245 LOG(DEBUG) << "Destroying: " << dir;
246 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
247 PLOG(ERROR) << "Failed to destroy " << dir;
248 return false;
249 }
250 return true;
251}
252
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000253// NB this assumes that there is only one thread listening for crypt commands, because
254// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000255static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100256 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700257
258 if(is_wrapped_key_supported()) {
259 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
260 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
261 } else {
262 if (!android::vold::randomKey(&de_key)) return false;
263 if (!android::vold::randomKey(&ce_key)) return false;
264 }
265
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000266 if (create_ephemeral) {
267 // If the key should be created as ephemeral, don't store it.
268 s_ephemeral_users.insert(user_id);
269 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700270 auto const directory_path = get_ce_key_directory_path(user_id);
271 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
272 auto const paths = get_ce_key_paths(directory_path);
273 std::string ce_key_path;
274 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700275 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
276 ce_key))
277 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700278 fixate_user_ce_key(directory_path, ce_key_path, paths);
279 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700280 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700281 kEmptyAuthentication, de_key))
282 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100283 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700284
285 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000286 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000287 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700288
289 if (is_wrapped_key_supported()) {
290 KeyBuffer ephemeral_wrapped_de_key;
291 KeyBuffer ephemeral_wrapped_ce_key;
292
293 /* Export and install the DE keys */
294 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
295 LOG(ERROR) << "Failed to export de_key";
296 return false;
297 }
298 /* Export and install the CE keys */
299 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
300 LOG(ERROR) << "Failed to export de_key";
301 return false;
302 }
303
304 de_key = std::move(ephemeral_wrapped_de_key);
305 ce_key = std::move(ephemeral_wrapped_ce_key);
306 }
307 if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700308 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700309 s_ce_keys[user_id] = std::move(ce_key);
310
311 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000312 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700313
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000314 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000315 return true;
316}
317
Paul Crowleydf528a72016-03-09 09:31:37 -0800318static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
319 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000320 auto refi = key_map.find(user_id);
321 if (refi == key_map.end()) {
322 LOG(ERROR) << "Cannot find key for " << user_id;
323 return false;
324 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800325 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000326 return true;
327}
328
Paul Crowley3aa914d2017-10-09 16:35:51 -0700329static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800330 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
331 if (entry == nullptr) {
332 return;
333 }
334 key_ref->contents_mode = entry->file_contents_mode;
335 key_ref->filenames_mode = entry->file_names_mode;
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700336}
337
Paul Crowley3aa914d2017-10-09 16:35:51 -0700338static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700339 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700340 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
341 key_ref.filenames_mode.c_str()) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100342}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100343
Paul Crowleydf528a72016-03-09 09:31:37 -0800344static bool is_numeric(const char* name) {
345 for (const char* p = name; *p != '\0'; p++) {
346 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000347 }
348 return true;
349}
350
351static bool load_all_de_keys() {
352 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800353 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000354 if (!dirp) {
355 PLOG(ERROR) << "Unable to read de key directory";
356 return false;
357 }
358 for (;;) {
359 errno = 0;
360 auto entry = readdir(dirp.get());
361 if (!entry) {
362 if (errno) {
363 PLOG(ERROR) << "Unable to read de key directory";
364 return false;
365 }
366 break;
367 }
368 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
369 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
370 continue;
371 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600372 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000373 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000374 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100375 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800376 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000377 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700378 if (is_wrapped_key_supported()) {
379 KeyBuffer ephemeral_wrapped_key;
380 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
381 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
382 return false;
383 }
384 key = std::move(ephemeral_wrapped_key);
385 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700386 if (!android::vold::installKey(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000387 s_de_key_raw_refs[user_id] = raw_ref;
388 LOG(DEBUG) << "Installed de key for user " << user_id;
389 }
390 }
Eric Biggersa701c452018-10-23 13:06:55 -0700391 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000392 // correct policy set on them, and that no rogue ones exist.
393 return true;
394}
395
Eric Biggersa701c452018-10-23 13:06:55 -0700396bool fscrypt_initialize_global_de() {
397 LOG(INFO) << "fscrypt_initialize_global_de";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700398 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800399
Paul Crowley38132a12016-02-09 09:50:32 +0000400 if (s_global_de_initialized) {
401 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000402 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800403 }
404
Paul Crowley3aa914d2017-10-09 16:35:51 -0700405 PolicyKeyRef device_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700406 wrapped_key_supported = is_wrapped_key_supported();
407
408 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication,
409 device_key_path, device_key_temp,
410 &device_ref.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700411 return false;
412 get_data_file_encryption_modes(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800413
Paul Crowley3aa914d2017-10-09 16:35:51 -0700414 std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
Eric Biggersa701c452018-10-23 13:06:55 -0700415 std::string mode_filename = std::string("/data") + fscrypt_key_mode;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800416 if (!android::base::WriteStringToFile(modestring, mode_filename)) {
Paul Lawrence6e410592016-05-24 14:20:38 -0700417 PLOG(ERROR) << "Cannot save type";
418 return false;
419 }
420
Eric Biggersa701c452018-10-23 13:06:55 -0700421 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700422 if (!android::base::WriteStringToFile(device_ref.key_raw_ref, ref_filename)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700423 PLOG(ERROR) << "Cannot save key reference to:" << ref_filename;
Paul Crowley76107cb2016-02-09 10:04:39 +0000424 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800425 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700426 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800427
Paul Crowley38132a12016-02-09 09:50:32 +0000428 s_global_de_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000429 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800430}
431
Eric Biggersa701c452018-10-23 13:06:55 -0700432bool fscrypt_init_user0() {
433 LOG(DEBUG) << "fscrypt_init_user0";
434 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000435 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
436 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
437 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700438 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000439 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000440 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700441 // TODO: switch to loading only DE_0 here once framework makes
442 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000443 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000444 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700445 // We can only safely prepare DE storage here, since CE keys are probably
446 // entangled with user credentials. The framework will always prepare CE
447 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700448 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700449 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000450 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700451 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700452
453 // If this is a non-FBE device that recently left an emulated mode,
454 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700455 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
456 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700457 }
458
Paul Crowley76107cb2016-02-09 10:04:39 +0000459 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000460}
461
Eric Biggersa701c452018-10-23 13:06:55 -0700462bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
463 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
464 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000465 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000466 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000467 // FIXME test for existence of key that is not loaded yet
468 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700469 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800470 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000471 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000472 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800473 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000474 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000475 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000476 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000477 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800478}
479
Pavel Grafovb350ed02017-07-27 17:34:57 +0100480static void drop_caches() {
481 // Clean any dirty pages (otherwise they won't be dropped).
482 sync();
483 // Drop inode and page caches.
484 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
485 PLOG(ERROR) << "Failed to drop caches during key eviction";
486 }
487}
488
Andrew Scull7ec25c72016-10-31 10:28:25 +0000489static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000490 bool success = true;
491 std::string raw_ref;
492 // If we haven't loaded the CE key, no need to evict it.
493 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700494 success &= android::vold::evictKey(raw_ref);
Pavel Grafovb350ed02017-07-27 17:34:57 +0100495 drop_caches();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000496 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700497
498 if(is_wrapped_key_supported()) {
499 KeyBuffer key;
500 key = s_ce_keys[user_id];
501
502 std::string keystr(key.data(), key.size());
503 Keymaster keymaster;
504
505 if (!keymaster) {
506 s_ce_keys.erase(user_id);
507 s_ce_key_raw_refs.erase(user_id);
508 return false;
509 }
510 keymaster.deleteKey(keystr);
511 }
512
513 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000514 s_ce_key_raw_refs.erase(user_id);
515 return success;
516}
517
Eric Biggersa701c452018-10-23 13:06:55 -0700518bool fscrypt_destroy_user_key(userid_t user_id) {
519 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
520 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000521 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000522 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000523 bool success = true;
524 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000525 success &= evict_ce_key(user_id);
Paul Crowley14c8c072018-09-18 13:30:21 -0700526 success &=
527 lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000528 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000529 auto it = s_ephemeral_users.find(user_id);
530 if (it != s_ephemeral_users.end()) {
531 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000532 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700533 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700534 success &= android::vold::destroyKey(path);
535 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700536 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700537 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700538 success &= android::vold::destroyKey(de_key_path);
539 } else {
540 LOG(INFO) << "Not present so not erasing: " << de_key_path;
541 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100542 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000543 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100544}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800545
Paul Crowley76107cb2016-02-09 10:04:39 +0000546static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700547 if (chmod(path.c_str(), 0000) != 0) {
548 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000549 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700550 }
551#if EMULATED_USES_SELINUX
552 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
553 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000554 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700555 }
556#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000557 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700558}
559
Paul Crowley76107cb2016-02-09 10:04:39 +0000560static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700561 if (chmod(path.c_str(), mode) != 0) {
562 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000563 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700564 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700565 }
566#if EMULATED_USES_SELINUX
567 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
568 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000569 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700570 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700571 }
572#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000573 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700574}
575
Paul Crowley3b71fc52017-10-09 10:55:21 -0700576static bool parse_hex(const std::string& hex, std::string* result) {
577 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800578 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000579 return true;
580 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800581 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800582 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000583 return false;
584 }
585 return true;
586}
587
Paul Crowley3aa914d2017-10-09 16:35:51 -0700588static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
589 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
590}
591
Paul Crowley26a53882017-10-26 11:16:39 -0700592static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
593 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
594}
595
Paul Crowley3aa914d2017-10-09 16:35:51 -0700596static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
597 PolicyKeyRef* key_ref) {
Paul Crowley26a53882017-10-26 11:16:39 -0700598 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
599 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700600 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700601 if (android::vold::pathExists(secdiscardable_path)) {
602 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
603 return false;
604 } else {
605 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
606 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
607 return false;
608 }
609 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
610 return false;
611 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700612 auto key_path = volkey_path(misc_path, volume_uuid);
613 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
614 PLOG(ERROR) << "Creating directories for: " << key_path;
615 return false;
616 }
Paul Crowley26a53882017-10-26 11:16:39 -0700617 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700618 wrapped_key_supported = is_wrapped_key_supported_external();
619
Paul Crowley26a53882017-10-26 11:16:39 -0700620 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700621 &key_ref->key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700622 return false;
623 key_ref->contents_mode =
624 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
625 key_ref->filenames_mode =
626 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
627 return true;
628}
629
630static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700631 auto path = volkey_path(misc_path, volume_uuid);
632 if (!android::vold::pathExists(path)) return true;
633 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700634}
635
Eric Biggersa701c452018-10-23 13:06:55 -0700636bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700637 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700638 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700639 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700640 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000641 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700642 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800643 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700644 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700645 auto auth =
646 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700647 auto const directory_path = get_ce_key_directory_path(user_id);
648 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700649
650 KeyBuffer ce_key;
651 if(is_wrapped_key_supported()) {
652 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
653 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
654 LOG(DEBUG) << "Successfully retrieved key";
655 } else {
656 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
657 LOG(DEBUG) << "Successfully retrieved key";
658 }
659 }
660 } else {
661 auto it = s_ce_keys.find(user_id);
662 if (it == s_ce_keys.end()) {
663 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
664 return false;
665 }
666 ce_key = it->second;
667 }
668
Paul Crowleya3630362016-05-17 14:17:56 -0700669 std::string ce_key_path;
670 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700671 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800672 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700673 return true;
674}
675
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800676bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700677 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800678 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700679 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800680 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700681 if (s_ephemeral_users.count(user_id) != 0) return true;
682 std::string token, secret;
683
684 if (!parse_hex(token_hex, &token)) return false;
685 if (!parse_hex(secret_hex, &secret)) return false;
686
687 if (is_wrapped_key_supported()) {
688 auto const directory_path = get_ce_key_directory_path(user_id);
689 auto const paths = get_ce_key_paths(directory_path);
690
691 KeyBuffer ce_key;
692 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
693
694 auto auth = android::vold::KeyAuthentication(token, secret);
695 /* Retrieve key while removing a pin. A secret is needed */
696 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
697 LOG(DEBUG) << "Successfully retrieved key";
698 } else {
699 /* Retrieve key when going None to swipe and vice versa when a
700 synthetic password is present */
701 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
702 LOG(DEBUG) << "Successfully retrieved key";
703 }
704 }
705
706 std::string ce_key_path;
707 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
708 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
709 return false;
710 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800711 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700712 }
713 return true;
714}
715
Eric Biggersa701c452018-10-23 13:06:55 -0700716bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
717 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
718 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700719 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700720 auto const directory_path = get_ce_key_directory_path(user_id);
721 auto const paths = get_ce_key_paths(directory_path);
722 if (paths.empty()) {
723 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
724 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000725 }
Paul Crowleya3630362016-05-17 14:17:56 -0700726 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000727 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000728}
729
Jeff Sharkey47695b22016-02-01 17:02:29 -0700730// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700731bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700732 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700733 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700734 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700735 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000736 if (s_ce_key_raw_refs.count(user_id) != 0) {
737 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000738 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000739 }
740 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800741 if (!parse_hex(token_hex, &token)) return false;
742 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000743 android::vold::KeyAuthentication auth(token, secret);
744 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000745 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000746 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800747 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700748 } else {
749 // When in emulation mode, we just use chmod. However, we also
750 // unlock directories when not in emulation mode, to bring devices
751 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000752 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800753 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700754 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
755 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700756 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000757 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700758 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800759 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000760 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800761}
762
Jeff Sharkey47695b22016-02-01 17:02:29 -0700763// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700764bool fscrypt_lock_user_key(userid_t user_id) {
765 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
766 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000767 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700768 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800769 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000770 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800771 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700772 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
773 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000774 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000775 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800776 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800777 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700778
Paul Crowley76107cb2016-02-09 10:04:39 +0000779 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800780}
781
Paul Crowley82b41ff2017-10-20 08:17:54 -0700782static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
783 userid_t user_id, int flags) {
784 if (0 != android::vold::ForkExecvp(
785 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
786 std::to_string(user_id), std::to_string(flags)})) {
787 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700788 return false;
789 }
790 return true;
791}
792
Eric Biggersa701c452018-10-23 13:06:55 -0700793bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700794 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700795 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800796 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700797
Paul Crowley82b41ff2017-10-20 08:17:54 -0700798 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600799 // DE_sys key
800 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
801 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
802 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600803
804 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700805 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
806 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800807 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700808 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
809
Paul Crowley3b71fc52017-10-09 10:55:21 -0700810 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700811 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600812#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700813 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700814 multiuser_get_uid(user_id, AID_EVERYBODY)))
815 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600816#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700817 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600818
Paul Crowley6b756ce2017-10-05 14:07:09 -0700819 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
820 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800821 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700822 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000823 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000824
Eric Biggersa701c452018-10-23 13:06:55 -0700825 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700826 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700827 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700828 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
829 get_data_file_encryption_modes(&de_ref);
830 if (!ensure_policy(de_ref, system_de_path)) return false;
831 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800832 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700833 } else {
834 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700835 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700836 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700837 }
Paul Crowley285956f2016-01-20 13:12:38 +0000838 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800839
Paul Crowley82b41ff2017-10-20 08:17:54 -0700840 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600841 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700842 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
843 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800844 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600845 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
846 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800847
Paul Crowley3b71fc52017-10-09 10:55:21 -0700848 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700849 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
850 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800851 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700852 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000853 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
854 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700855
Eric Biggersa701c452018-10-23 13:06:55 -0700856 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700857 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700858 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700859 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
860 get_data_file_encryption_modes(&ce_ref);
861 if (!ensure_policy(ce_ref, system_ce_path)) return false;
862 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800863 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700864
Paul Crowley3aa914d2017-10-09 16:35:51 -0700865 } else {
866 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700867 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700868 if (!ensure_policy(ce_ref, media_ce_path)) return false;
869 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700870 }
Paul Crowley1a965262017-10-13 13:49:50 -0700871
872 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700873 // Now that credentials have been installed, we can run restorecon
874 // over these paths
875 // NOTE: these paths need to be kept in sync with libselinux
876 android::vold::RestoreconRecursive(system_ce_path);
877 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700878 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800879 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700880 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800881
Paul Crowley76107cb2016-02-09 10:04:39 +0000882 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800883}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600884
Eric Biggersa701c452018-10-23 13:06:55 -0700885bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
886 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600887 << ", user " << user_id << ", flags " << flags;
888 bool res = true;
889
Paul Crowley82b41ff2017-10-20 08:17:54 -0700890 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
891
892 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700893 // CE_n key
894 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
895 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800896 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700897 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
898 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
899
900 res &= destroy_dir(media_ce_path);
901 res &= destroy_dir(user_ce_path);
902 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700903 res &= destroy_dir(system_ce_path);
904 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800905 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700906 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700907 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700908 res &= destroy_volkey(misc_ce_path, volume_uuid);
909 }
Paul Crowley8e550662017-10-16 17:01:44 -0700910 }
911 }
912
Paul Crowley82b41ff2017-10-20 08:17:54 -0700913 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600914 // DE_sys key
915 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
916 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
917 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600918
919 // DE_n key
920 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
921 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800922 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600923 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
924
Paul Crowley8e550662017-10-16 17:01:44 -0700925 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700926 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600927 res &= destroy_dir(system_legacy_path);
928#if MANAGE_MISC_DIRS
929 res &= destroy_dir(misc_legacy_path);
930#endif
931 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600932 res &= destroy_dir(system_de_path);
933 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800934 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700935 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700936 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700937 res &= destroy_volkey(misc_de_path, volume_uuid);
938 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600939 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600940 }
941
942 return res;
943}
Rubin Xu2436e272017-04-27 20:43:10 +0100944
Paul Crowleyc6433a22017-10-24 14:54:43 -0700945static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
946 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
947 if (!dirp) {
948 PLOG(ERROR) << "Unable to open directory: " + directory_path;
949 return false;
950 }
951 bool res = true;
952 for (;;) {
953 errno = 0;
954 auto const entry = readdir(dirp.get());
955 if (!entry) {
956 if (errno) {
957 PLOG(ERROR) << "Unable to read directory: " + directory_path;
958 return false;
959 }
960 break;
961 }
962 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
963 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
964 continue;
965 }
966 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
967 }
968 return res;
969}
970
Eric Biggersa701c452018-10-23 13:06:55 -0700971bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700972 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700973 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700974 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
975 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700976 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
977 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
978 return res;
979}