blob: a84b497431b6764f2229e09dda82799e90e00057 [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"
Gaurav Kashyap38cbb942019-07-17 18:11:57 -070024#include "model/Disk.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070025
Paul Crowleya3630362016-05-17 14:17:56 -070026#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000027#include <map>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000028#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070029#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080030#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000032
Paul Crowleydf528a72016-03-09 09:31:37 -080033#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070036#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070037#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080038#include <sys/mount.h>
39#include <sys/stat.h>
40#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000042
Paul Crowley480fcd22015-08-24 14:53:28 +010043#include <private/android_filesystem_config.h>
44
Paul Crowley82b41ff2017-10-20 08:17:54 -070045#include "android/os/IVold.h"
46
Paul Lawrence731a7a22015-04-28 22:14:15 +000047#include "cryptfs.h"
48
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060050#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070051
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080052#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070053#include <cutils/properties.h>
54
Eric Biggersa701c452018-10-23 13:06:55 -070055#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070056#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080057
Elliott Hughes7e128fb2015-12-04 15:50:53 -080058#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080059#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070060#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080061#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070062#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080063#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000064
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080065using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080066using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000067using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010068using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070069using android::vold::Keymaster;
70using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080071using android::vold::writeStringToFile;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080072
Paul Lawrence731a7a22015-04-28 22:14:15 +000073namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000074
Paul Crowley3aa914d2017-10-09 16:35:51 -070075struct PolicyKeyRef {
76 std::string contents_mode;
77 std::string filenames_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -070078 int policy_version;
Paul Crowley3aa914d2017-10-09 16:35:51 -070079 std::string key_raw_ref;
Eric Biggers83a73d72019-09-30 13:06:47 -070080
81 PolicyKeyRef() : policy_version(0) {}
Paul Crowley3aa914d2017-10-09 16:35:51 -070082};
83
Eric Biggersa701c452018-10-23 13:06:55 -070084const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080085const std::string device_key_path = device_key_dir + "/key";
86const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080087
Paul Crowleydf528a72016-03-09 09:31:37 -080088const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
89const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070090const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000091
Paul Crowley26a53882017-10-26 11:16:39 -070092const std::string systemwide_volume_key_dir =
93 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
94
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070095bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080096
Paul Crowleydf528a72016-03-09 09:31:37 -080097// Some users are ephemeral, don't try to wipe their keys from disk
98std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080099
Paul Crowleydf528a72016-03-09 09:31:37 -0800100// Map user ids to key references
101std::map<userid_t, std::string> s_de_key_raw_refs;
102std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -0700103// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +0100104std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000105
Paul Crowley14c8c072018-09-18 13:30:21 -0700106} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +0000107
Eric Biggersa701c452018-10-23 13:06:55 -0700108static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000109 return property_get_bool("persist.sys.emulate_fbe", false);
110}
111
Paul Crowley3b71fc52017-10-09 10:55:21 -0700112static const char* escape_empty(const std::string& value) {
113 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000114}
115
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000116static std::string get_de_key_path(userid_t user_id) {
117 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
118}
119
Paul Crowleya3630362016-05-17 14:17:56 -0700120static std::string get_ce_key_directory_path(userid_t user_id) {
121 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
122}
123
124// Returns the keys newest first
125static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
126 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
127 if (!dirp) {
128 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
129 return std::vector<std::string>();
130 }
131 std::vector<std::string> result;
132 for (;;) {
133 errno = 0;
134 auto const entry = readdir(dirp.get());
135 if (!entry) {
136 if (errno) {
137 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
138 return std::vector<std::string>();
139 }
140 break;
141 }
142 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
143 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
144 continue;
145 }
146 result.emplace_back(directory_path + "/" + entry->d_name);
147 }
148 std::sort(result.begin(), result.end());
149 std::reverse(result.begin(), result.end());
150 return result;
151}
152
153static std::string get_ce_key_current_path(const std::string& directory_path) {
154 return directory_path + "/current";
155}
156
157static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700158 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700159 if (paths.empty()) {
160 *ce_key_path = get_ce_key_current_path(directory_path);
161 return true;
162 }
163 for (unsigned int i = 0; i < UINT_MAX; i++) {
164 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
165 if (paths[0] < candidate) {
166 *ce_key_path = candidate;
167 return true;
168 }
169 }
170 return false;
171}
172
173// Discard all keys but the named one; rename it to canonical name.
174// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700175static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700176 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700177 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700178 if (other_path != to_fix) {
179 android::vold::destroyKey(other_path);
180 }
181 }
182 auto const current_path = get_ce_key_current_path(directory_path);
183 if (to_fix != current_path) {
184 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
185 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
186 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800187 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700188 }
189 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800190 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700191}
192
193static bool read_and_fixate_user_ce_key(userid_t user_id,
194 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700195 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700196 auto const directory_path = get_ce_key_directory_path(user_id);
197 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700198 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700199 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
200 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
201 LOG(DEBUG) << "Successfully retrieved key";
202 fixate_user_ce_key(directory_path, ce_key_path, paths);
203 return true;
204 }
205 }
206 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
207 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100208}
209
Eric Biggers83a73d72019-09-30 13:06:47 -0700210// Retrieve the options to use for encryption policies on the /data filesystem.
211static void get_data_file_encryption_options(PolicyKeyRef* key_ref) {
212 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
213 if (entry == nullptr) {
214 return;
215 }
216 key_ref->contents_mode = entry->file_contents_mode;
217 key_ref->filenames_mode = entry->file_names_mode;
218 key_ref->policy_version = entry->file_policy_version;
219}
220
221// Retrieve the version to use for encryption policies on the /data filesystem.
222static int get_data_file_policy_version(void) {
223 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
224 if (entry == nullptr) {
225 return 0;
226 }
227 return entry->file_policy_version;
228}
229
230// Retrieve the options to use for encryption policies on adoptable storage.
231static bool get_volume_file_encryption_options(PolicyKeyRef* key_ref) {
232 key_ref->contents_mode =
233 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
234 key_ref->filenames_mode =
235 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
236 key_ref->policy_version = 1;
237
238 std::string raw_flags = android::base::GetProperty("ro.crypto.volume.flags", "");
239 auto flags = android::base::Split(raw_flags, "+");
240 for (const auto& flag : flags) {
241 if (flag == "v1") {
242 key_ref->policy_version = 1;
243 } else if (flag == "v2") {
244 key_ref->policy_version = 2;
245 } else {
246 LOG(ERROR) << "Unknown flag in ro.crypto.volume.flags: " << flag;
247 return false;
248 }
249 }
250 return true;
251}
252
Eric Biggersf3dc4202019-09-30 13:05:58 -0700253// Install a key for use by encrypted files on the /data filesystem.
254static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700255 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700256}
257
258// Evict a key for use by encrypted files on the /data filesystem.
259static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700260 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700261}
262
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700263bool is_ice_supported_external(int flags) {
264 /*
265 * Logic can be changed when more card controllers start supporting ICE.
266 * Until then, checking only for UFS card.
267 */
268 if ((flags & android::vold::Disk::Flags::kUfsCard) ==
269 android::vold::Disk::Flags::kUfsCard)
270 return true;
271 return false;
272}
273
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700274bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800275 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700276}
277
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700278bool is_wrapped_key_supported_external(int flags) {
279 if (is_ice_supported_external(flags))
280 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700281 return false;
282}
283
Neeraj Sonic480f912018-12-14 15:18:15 +0530284bool is_metadata_wrapped_key_supported() {
285 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
286}
287
Paul Crowleydf528a72016-03-09 09:31:37 -0800288static bool read_and_install_user_ce_key(userid_t user_id,
289 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000290 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100291 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700292 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000293 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700294
295 if (is_wrapped_key_supported()) {
296 KeyBuffer ephemeral_wrapped_key;
297 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
298 LOG(ERROR) << "Failed to export ce key";
299 return false;
300 }
301 ce_key = std::move(ephemeral_wrapped_key);
302 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700303 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100304 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000305 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000306 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000307 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000308}
309
Paul Crowleydf528a72016-03-09 09:31:37 -0800310static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000311 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000312 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
313 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000314 return false;
315 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000316 return true;
317}
318
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600319static bool destroy_dir(const std::string& dir) {
320 LOG(DEBUG) << "Destroying: " << dir;
321 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
322 PLOG(ERROR) << "Failed to destroy " << dir;
323 return false;
324 }
325 return true;
326}
327
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000328// NB this assumes that there is only one thread listening for crypt commands, because
329// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000330static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100331 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700332
333 if(is_wrapped_key_supported()) {
334 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
335 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
336 } else {
337 if (!android::vold::randomKey(&de_key)) return false;
338 if (!android::vold::randomKey(&ce_key)) return false;
339 }
340
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000341 if (create_ephemeral) {
342 // If the key should be created as ephemeral, don't store it.
343 s_ephemeral_users.insert(user_id);
344 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700345 auto const directory_path = get_ce_key_directory_path(user_id);
346 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
347 auto const paths = get_ce_key_paths(directory_path);
348 std::string ce_key_path;
349 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700350 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
351 ce_key))
352 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700353 fixate_user_ce_key(directory_path, ce_key_path, paths);
354 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700355 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700356 kEmptyAuthentication, de_key))
357 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100358 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700359
360 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000361 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000362 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700363
364 if (is_wrapped_key_supported()) {
365 KeyBuffer ephemeral_wrapped_de_key;
366 KeyBuffer ephemeral_wrapped_ce_key;
367
368 /* Export and install the DE keys */
369 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
370 LOG(ERROR) << "Failed to export de_key";
371 return false;
372 }
373 /* Export and install the CE keys */
374 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
375 LOG(ERROR) << "Failed to export de_key";
376 return false;
377 }
378
379 de_key = std::move(ephemeral_wrapped_de_key);
380 ce_key = std::move(ephemeral_wrapped_ce_key);
381 }
Steven Lavered747be2019-10-24 16:40:01 -0700382 if (!install_data_key(de_key, &de_raw_ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700383 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700384 s_ce_keys[user_id] = std::move(ce_key);
385
386 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000387 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700388
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000389 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000390 return true;
391}
392
Paul Crowleydf528a72016-03-09 09:31:37 -0800393static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
394 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000395 auto refi = key_map.find(user_id);
396 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700397 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000398 return false;
399 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800400 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000401 return true;
402}
403
Paul Crowley3aa914d2017-10-09 16:35:51 -0700404static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700405 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700406 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
Eric Biggers83a73d72019-09-30 13:06:47 -0700407 key_ref.filenames_mode.c_str(), key_ref.policy_version) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100408}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100409
Paul Crowleydf528a72016-03-09 09:31:37 -0800410static bool is_numeric(const char* name) {
411 for (const char* p = name; *p != '\0'; p++) {
412 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000413 }
414 return true;
415}
416
417static bool load_all_de_keys() {
418 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800419 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000420 if (!dirp) {
421 PLOG(ERROR) << "Unable to read de key directory";
422 return false;
423 }
424 for (;;) {
425 errno = 0;
426 auto entry = readdir(dirp.get());
427 if (!entry) {
428 if (errno) {
429 PLOG(ERROR) << "Unable to read de key directory";
430 return false;
431 }
432 break;
433 }
434 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
435 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
436 continue;
437 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600438 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000439 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000440 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100441 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800442 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000443 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700444 if (is_wrapped_key_supported()) {
445 KeyBuffer ephemeral_wrapped_key;
446 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
447 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
448 return false;
449 }
450 key = std::move(ephemeral_wrapped_key);
451 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700452 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000453 s_de_key_raw_refs[user_id] = raw_ref;
454 LOG(DEBUG) << "Installed de key for user " << user_id;
455 }
456 }
Eric Biggersa701c452018-10-23 13:06:55 -0700457 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000458 // correct policy set on them, and that no rogue ones exist.
459 return true;
460}
461
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700462bool fscrypt_initialize_systemwide_keys() {
463 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700464 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800465
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700466 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000467 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000468 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800469 }
470
Paul Crowley3aa914d2017-10-09 16:35:51 -0700471 PolicyKeyRef device_ref;
Eric Biggers83a73d72019-09-30 13:06:47 -0700472 get_data_file_encryption_options(&device_ref);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700473 wrapped_key_supported = is_wrapped_key_supported();
474
Eric Biggers83a73d72019-09-30 13:06:47 -0700475 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
476 device_key_temp, "", device_ref.policy_version,
Steven Lavered747be2019-10-24 16:40:01 -0700477 &device_ref.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700478 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800479
Eric Biggers83a73d72019-09-30 13:06:47 -0700480 std::string options_string =
481 StringPrintf("%s:%s:v%d", device_ref.contents_mode.c_str(),
482 device_ref.filenames_mode.c_str(), device_ref.policy_version);
483 std::string options_filename = std::string("/data") + fscrypt_key_mode;
484 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700485
Eric Biggersa701c452018-10-23 13:06:55 -0700486 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Tommy Chiua98464f2019-03-26 14:14:19 +0800487 if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700488 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800489
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700490 KeyBuffer per_boot_key;
491 if (!android::vold::randomKey(&per_boot_key)) return false;
492 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700493 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700494 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
495 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
496 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
497
Tommy Chiua98464f2019-03-26 14:14:19 +0800498 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700499 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000500 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800501}
502
Eric Biggersa701c452018-10-23 13:06:55 -0700503bool fscrypt_init_user0() {
504 LOG(DEBUG) << "fscrypt_init_user0";
505 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000506 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
507 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
508 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700509 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000510 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000511 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700512 // TODO: switch to loading only DE_0 here once framework makes
513 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000514 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000515 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700516 // We can only safely prepare DE storage here, since CE keys are probably
517 // entangled with user credentials. The framework will always prepare CE
518 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700519 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700520 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000521 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700522 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700523
524 // If this is a non-FBE device that recently left an emulated mode,
525 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700526 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
527 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700528 }
529
Paul Crowley76107cb2016-02-09 10:04:39 +0000530 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000531}
532
Eric Biggersa701c452018-10-23 13:06:55 -0700533bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
534 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
535 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000536 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000537 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000538 // FIXME test for existence of key that is not loaded yet
539 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700540 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800541 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000542 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000543 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800544 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000545 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000546 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000547 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000548 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800549}
550
Eric Biggersce368682019-04-03 15:44:06 -0700551// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700552// in the case where the keys are being put in the session keyring (rather in
553// the newer filesystem-level keyrings), because removing a key from the session
554// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
555// was already set up. So to remove the per-file keys and make the files
556// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700557//
558// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
559// objects systemwide. This is overkill, but it's the best available method
560// currently. Don't use drop_caches mode "3" because that also evicts pagecache
561// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700562static void drop_caches_if_needed() {
563 if (android::vold::isFsKeyringSupported()) {
564 return;
565 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100566 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700567 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100568 PLOG(ERROR) << "Failed to drop caches during key eviction";
569 }
570}
571
Andrew Scull7ec25c72016-10-31 10:28:25 +0000572static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000573 bool success = true;
574 std::string raw_ref;
575 // If we haven't loaded the CE key, no need to evict it.
576 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700577 success &= evict_data_key(raw_ref);
578 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000579 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700580
581 if(is_wrapped_key_supported()) {
582 KeyBuffer key;
583 key = s_ce_keys[user_id];
584
585 std::string keystr(key.data(), key.size());
586 Keymaster keymaster;
587
588 if (!keymaster) {
589 s_ce_keys.erase(user_id);
590 s_ce_key_raw_refs.erase(user_id);
591 return false;
592 }
593 keymaster.deleteKey(keystr);
594 }
595
596 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000597 s_ce_key_raw_refs.erase(user_id);
598 return success;
599}
600
Eric Biggersa701c452018-10-23 13:06:55 -0700601bool fscrypt_destroy_user_key(userid_t user_id) {
602 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
603 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000604 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000605 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000606 bool success = true;
607 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000608 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700609 success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_data_key(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000610 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000611 auto it = s_ephemeral_users.find(user_id);
612 if (it != s_ephemeral_users.end()) {
613 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000614 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700615 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700616 success &= android::vold::destroyKey(path);
617 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700618 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700619 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700620 success &= android::vold::destroyKey(de_key_path);
621 } else {
622 LOG(INFO) << "Not present so not erasing: " << de_key_path;
623 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100624 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000625 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100626}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800627
Paul Crowley76107cb2016-02-09 10:04:39 +0000628static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700629 if (chmod(path.c_str(), 0000) != 0) {
630 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000631 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700632 }
633#if EMULATED_USES_SELINUX
634 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
635 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000636 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700637 }
638#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000639 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700640}
641
Paul Crowley76107cb2016-02-09 10:04:39 +0000642static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700643 if (chmod(path.c_str(), mode) != 0) {
644 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000645 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700646 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700647 }
648#if EMULATED_USES_SELINUX
649 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
650 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000651 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700652 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700653 }
654#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000655 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700656}
657
Paul Crowley3b71fc52017-10-09 10:55:21 -0700658static bool parse_hex(const std::string& hex, std::string* result) {
659 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800660 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000661 return true;
662 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800663 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800664 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000665 return false;
666 }
667 return true;
668}
669
Paul Crowley3aa914d2017-10-09 16:35:51 -0700670static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
671 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
672}
673
Paul Crowley26a53882017-10-26 11:16:39 -0700674static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
675 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
676}
677
Paul Crowley3aa914d2017-10-09 16:35:51 -0700678static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700679 PolicyKeyRef* key_ref, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700680 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
681 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700682 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700683 if (android::vold::pathExists(secdiscardable_path)) {
684 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
685 return false;
686 } else {
687 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
688 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
689 return false;
690 }
691 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
692 return false;
693 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700694 auto key_path = volkey_path(misc_path, volume_uuid);
695 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
696 PLOG(ERROR) << "Creating directories for: " << key_path;
697 return false;
698 }
Paul Crowley26a53882017-10-26 11:16:39 -0700699 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700700
701 if (!get_volume_file_encryption_options(key_ref)) return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700702 wrapped_key_supported = is_wrapped_key_supported_external(flags);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700703
Paul Crowley26a53882017-10-26 11:16:39 -0700704 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Steven Lavered747be2019-10-24 16:40:01 -0700705 volume_uuid, key_ref->policy_version,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700706 &key_ref->key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700707 return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700708
709 if (is_ice_supported_external(flags)) {
710 key_ref->contents_mode =
711 android::base::GetProperty("ro.crypto.volume.contents_mode", "ice");
712 } else {
713 key_ref->contents_mode =
714 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
715 }
716
Paul Crowley3aa914d2017-10-09 16:35:51 -0700717 key_ref->filenames_mode =
718 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
719 return true;
720}
721
722static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700723 auto path = volkey_path(misc_path, volume_uuid);
724 if (!android::vold::pathExists(path)) return true;
725 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700726}
727
Eric Biggersa701c452018-10-23 13:06:55 -0700728bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700729 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700730 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700731 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700732 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000733 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700734 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800735 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700736 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700737 auto auth =
738 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700739 auto const directory_path = get_ce_key_directory_path(user_id);
740 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700741
742 KeyBuffer ce_key;
743 if(is_wrapped_key_supported()) {
744 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
745 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
746 LOG(DEBUG) << "Successfully retrieved key";
747 } else {
748 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
749 LOG(DEBUG) << "Successfully retrieved key";
750 }
751 }
752 } else {
753 auto it = s_ce_keys.find(user_id);
754 if (it == s_ce_keys.end()) {
755 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
756 return false;
757 }
758 ce_key = it->second;
759 }
760
Paul Crowleya3630362016-05-17 14:17:56 -0700761 std::string ce_key_path;
762 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700763 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800764 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700765 return true;
766}
767
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800768bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700769 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800770 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700771 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800772 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700773 if (s_ephemeral_users.count(user_id) != 0) return true;
774 std::string token, secret;
775
776 if (!parse_hex(token_hex, &token)) return false;
777 if (!parse_hex(secret_hex, &secret)) return false;
778
779 if (is_wrapped_key_supported()) {
780 auto const directory_path = get_ce_key_directory_path(user_id);
781 auto const paths = get_ce_key_paths(directory_path);
782
783 KeyBuffer ce_key;
784 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
785
786 auto auth = android::vold::KeyAuthentication(token, secret);
787 /* Retrieve key while removing a pin. A secret is needed */
788 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
789 LOG(DEBUG) << "Successfully retrieved key";
790 } else {
791 /* Retrieve key when going None to swipe and vice versa when a
792 synthetic password is present */
793 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
794 LOG(DEBUG) << "Successfully retrieved key";
795 }
796 }
797
798 std::string ce_key_path;
799 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
800 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
801 return false;
802 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800803 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700804 }
805 return true;
806}
807
Eric Biggersa701c452018-10-23 13:06:55 -0700808bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
809 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
810 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700811 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700812 auto const directory_path = get_ce_key_directory_path(user_id);
813 auto const paths = get_ce_key_paths(directory_path);
814 if (paths.empty()) {
815 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
816 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000817 }
Paul Crowleya3630362016-05-17 14:17:56 -0700818 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000819 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000820}
821
Jeff Sharkey47695b22016-02-01 17:02:29 -0700822// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700823bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700824 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700825 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700826 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700827 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000828 if (s_ce_key_raw_refs.count(user_id) != 0) {
829 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000830 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000831 }
832 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800833 if (!parse_hex(token_hex, &token)) return false;
834 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000835 android::vold::KeyAuthentication auth(token, secret);
836 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000837 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000838 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800839 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700840 } else {
841 // When in emulation mode, we just use chmod. However, we also
842 // unlock directories when not in emulation mode, to bring devices
843 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000844 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800845 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700846 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
847 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700848 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000849 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700850 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800851 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000852 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800853}
854
Jeff Sharkey47695b22016-02-01 17:02:29 -0700855// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700856bool fscrypt_lock_user_key(userid_t user_id) {
857 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
858 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000859 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700860 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800861 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000862 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800863 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700864 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
865 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000866 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000867 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800868 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800869 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700870
Paul Crowley76107cb2016-02-09 10:04:39 +0000871 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800872}
873
Paul Crowley82b41ff2017-10-20 08:17:54 -0700874static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
875 userid_t user_id, int flags) {
876 if (0 != android::vold::ForkExecvp(
877 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
878 std::to_string(user_id), std::to_string(flags)})) {
879 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700880 return false;
881 }
882 return true;
883}
884
Eric Biggersa701c452018-10-23 13:06:55 -0700885bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700886 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700887 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800888 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700889
Paul Crowley82b41ff2017-10-20 08:17:54 -0700890 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600891 // DE_sys key
892 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
893 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
894 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600895
896 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700897 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
898 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800899 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700900 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
901
Paul Crowley3b71fc52017-10-09 10:55:21 -0700902 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700903 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600904#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700905 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700906 multiuser_get_uid(user_id, AID_EVERYBODY)))
907 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600908#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700909 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600910
Paul Crowley6b756ce2017-10-05 14:07:09 -0700911 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
912 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800913 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700914 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000915 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000916
Eric Biggersa701c452018-10-23 13:06:55 -0700917 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700918 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700919 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700920 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700921 get_data_file_encryption_options(&de_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700922 if (!ensure_policy(de_ref, system_de_path)) return false;
923 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800924 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700925 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700926 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700927 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700928 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700929 }
Paul Crowley285956f2016-01-20 13:12:38 +0000930 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800931
Paul Crowley82b41ff2017-10-20 08:17:54 -0700932 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600933 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700934 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
935 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800936 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600937 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
938 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800939
Paul Crowley3b71fc52017-10-09 10:55:21 -0700940 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700941 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
942 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800943 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700944 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000945 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
946 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700947
Eric Biggersa701c452018-10-23 13:06:55 -0700948 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700949 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700950 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700951 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700952 get_data_file_encryption_options(&ce_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700953 if (!ensure_policy(ce_ref, system_ce_path)) return false;
954 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800955 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700956
Paul Crowley3aa914d2017-10-09 16:35:51 -0700957 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700958 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700959 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700960 if (!ensure_policy(ce_ref, media_ce_path)) return false;
961 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700962 }
Paul Crowley1a965262017-10-13 13:49:50 -0700963
964 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700965 // Now that credentials have been installed, we can run restorecon
966 // over these paths
967 // NOTE: these paths need to be kept in sync with libselinux
968 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700969 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700970 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700971 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800972 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700973 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800974
Paul Crowley76107cb2016-02-09 10:04:39 +0000975 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800976}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600977
Eric Biggersa701c452018-10-23 13:06:55 -0700978bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
979 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600980 << ", user " << user_id << ", flags " << flags;
981 bool res = true;
982
Paul Crowley82b41ff2017-10-20 08:17:54 -0700983 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
984
985 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700986 // CE_n key
987 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
988 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800989 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700990 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
991 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
992
993 res &= destroy_dir(media_ce_path);
994 res &= destroy_dir(user_ce_path);
995 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700996 res &= destroy_dir(system_ce_path);
997 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800998 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700999 } else {
Eric Biggersa701c452018-10-23 13:06:55 -07001000 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -07001001 res &= destroy_volkey(misc_ce_path, volume_uuid);
1002 }
Paul Crowley8e550662017-10-16 17:01:44 -07001003 }
1004 }
1005
Paul Crowley82b41ff2017-10-20 08:17:54 -07001006 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001007 // DE_sys key
1008 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
1009 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
1010 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001011
1012 // DE_n key
1013 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
1014 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -08001015 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001016 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
1017
Paul Crowley8e550662017-10-16 17:01:44 -07001018 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -07001019 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001020 res &= destroy_dir(system_legacy_path);
1021#if MANAGE_MISC_DIRS
1022 res &= destroy_dir(misc_legacy_path);
1023#endif
1024 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001025 res &= destroy_dir(system_de_path);
1026 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -08001027 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -07001028 } else {
Eric Biggersa701c452018-10-23 13:06:55 -07001029 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -07001030 res &= destroy_volkey(misc_de_path, volume_uuid);
1031 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001032 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -06001033 }
1034
1035 return res;
1036}
Rubin Xu2436e272017-04-27 20:43:10 +01001037
Paul Crowleyc6433a22017-10-24 14:54:43 -07001038static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
1039 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
1040 if (!dirp) {
1041 PLOG(ERROR) << "Unable to open directory: " + directory_path;
1042 return false;
1043 }
1044 bool res = true;
1045 for (;;) {
1046 errno = 0;
1047 auto const entry = readdir(dirp.get());
1048 if (!entry) {
1049 if (errno) {
1050 PLOG(ERROR) << "Unable to read directory: " + directory_path;
1051 return false;
1052 }
1053 break;
1054 }
1055 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
1056 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
1057 continue;
1058 }
1059 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
1060 }
1061 return res;
1062}
1063
Eric Biggersa701c452018-10-23 13:06:55 -07001064bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -07001065 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -07001066 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -07001067 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
1068 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -07001069 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
1070 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
1071 return res;
1072}