blob: e97b5405f22b2e6a359548f6e086d00c27186844 [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>
Jieb6698d52018-11-12 15:26:02 +080062#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000063
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080064using android::base::StringPrintf;
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;
Tommy Chiua98464f2019-03-26 14:14:19 +080070using android::vold::writeStringToFile;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080071
Paul Lawrence731a7a22015-04-28 22:14:15 +000072namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000073
Paul Crowley3aa914d2017-10-09 16:35:51 -070074struct PolicyKeyRef {
75 std::string contents_mode;
76 std::string filenames_mode;
77 std::string key_raw_ref;
78};
79
Eric Biggersa701c452018-10-23 13:06:55 -070080const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080081const std::string device_key_path = device_key_dir + "/key";
82const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080083
Paul Crowleydf528a72016-03-09 09:31:37 -080084const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
85const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070086const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000087
Paul Crowley26a53882017-10-26 11:16:39 -070088const std::string systemwide_volume_key_dir =
89 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
90
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070091bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080092
Paul Crowleydf528a72016-03-09 09:31:37 -080093// Some users are ephemeral, don't try to wipe their keys from disk
94std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080095
Paul Crowleydf528a72016-03-09 09:31:37 -080096// Map user ids to key references
97std::map<userid_t, std::string> s_de_key_raw_refs;
98std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070099// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +0100100std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000101
Paul Crowley14c8c072018-09-18 13:30:21 -0700102} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +0000103
Eric Biggersa701c452018-10-23 13:06:55 -0700104static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000105 return property_get_bool("persist.sys.emulate_fbe", false);
106}
107
Paul Crowley3b71fc52017-10-09 10:55:21 -0700108static const char* escape_empty(const std::string& value) {
109 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000110}
111
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000112static std::string get_de_key_path(userid_t user_id) {
113 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
114}
115
Paul Crowleya3630362016-05-17 14:17:56 -0700116static std::string get_ce_key_directory_path(userid_t user_id) {
117 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
118}
119
120// Returns the keys newest first
121static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
122 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
123 if (!dirp) {
124 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
125 return std::vector<std::string>();
126 }
127 std::vector<std::string> result;
128 for (;;) {
129 errno = 0;
130 auto const entry = readdir(dirp.get());
131 if (!entry) {
132 if (errno) {
133 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
134 return std::vector<std::string>();
135 }
136 break;
137 }
138 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
139 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
140 continue;
141 }
142 result.emplace_back(directory_path + "/" + entry->d_name);
143 }
144 std::sort(result.begin(), result.end());
145 std::reverse(result.begin(), result.end());
146 return result;
147}
148
149static std::string get_ce_key_current_path(const std::string& directory_path) {
150 return directory_path + "/current";
151}
152
153static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700154 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700155 if (paths.empty()) {
156 *ce_key_path = get_ce_key_current_path(directory_path);
157 return true;
158 }
159 for (unsigned int i = 0; i < UINT_MAX; i++) {
160 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
161 if (paths[0] < candidate) {
162 *ce_key_path = candidate;
163 return true;
164 }
165 }
166 return false;
167}
168
169// Discard all keys but the named one; rename it to canonical name.
170// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700171static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700172 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700173 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700174 if (other_path != to_fix) {
175 android::vold::destroyKey(other_path);
176 }
177 }
178 auto const current_path = get_ce_key_current_path(directory_path);
179 if (to_fix != current_path) {
180 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
181 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
182 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800183 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700184 }
185 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800186 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700187}
188
189static bool read_and_fixate_user_ce_key(userid_t user_id,
190 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700191 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700192 auto const directory_path = get_ce_key_directory_path(user_id);
193 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700194 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700195 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
196 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
197 LOG(DEBUG) << "Successfully retrieved key";
198 fixate_user_ce_key(directory_path, ce_key_path, paths);
199 return true;
200 }
201 }
202 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
203 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100204}
205
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700206bool is_ice_supported_external(int flags) {
207 /*
208 * Logic can be changed when more card controllers start supporting ICE.
209 * Until then, checking only for UFS card.
210 */
211 if ((flags & android::vold::Disk::Flags::kUfsCard) ==
212 android::vold::Disk::Flags::kUfsCard)
213 return true;
214 return false;
215}
216
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700217bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800218 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700219}
220
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700221bool is_wrapped_key_supported_external(int flags) {
222 if (is_ice_supported_external(flags))
223 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700224 return false;
225}
226
Neeraj Sonic480f912018-12-14 15:18:15 +0530227bool is_metadata_wrapped_key_supported() {
228 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
229}
230
Paul Crowleydf528a72016-03-09 09:31:37 -0800231static bool read_and_install_user_ce_key(userid_t user_id,
232 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000233 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100234 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700235 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000236 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700237
238 if (is_wrapped_key_supported()) {
239 KeyBuffer ephemeral_wrapped_key;
240 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
241 LOG(ERROR) << "Failed to export ce key";
242 return false;
243 }
244 ce_key = std::move(ephemeral_wrapped_key);
245 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700246 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100247 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000248 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000249 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000250 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000251}
252
Paul Crowleydf528a72016-03-09 09:31:37 -0800253static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000254 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000255 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
256 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000257 return false;
258 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000259 return true;
260}
261
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600262static bool destroy_dir(const std::string& dir) {
263 LOG(DEBUG) << "Destroying: " << dir;
264 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
265 PLOG(ERROR) << "Failed to destroy " << dir;
266 return false;
267 }
268 return true;
269}
270
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000271// NB this assumes that there is only one thread listening for crypt commands, because
272// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000273static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100274 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700275
276 if(is_wrapped_key_supported()) {
277 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
278 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
279 } else {
280 if (!android::vold::randomKey(&de_key)) return false;
281 if (!android::vold::randomKey(&ce_key)) return false;
282 }
283
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000284 if (create_ephemeral) {
285 // If the key should be created as ephemeral, don't store it.
286 s_ephemeral_users.insert(user_id);
287 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700288 auto const directory_path = get_ce_key_directory_path(user_id);
289 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
290 auto const paths = get_ce_key_paths(directory_path);
291 std::string ce_key_path;
292 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700293 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
294 ce_key))
295 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700296 fixate_user_ce_key(directory_path, ce_key_path, paths);
297 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700298 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700299 kEmptyAuthentication, de_key))
300 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100301 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700302
303 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000304 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000305 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700306
307 if (is_wrapped_key_supported()) {
308 KeyBuffer ephemeral_wrapped_de_key;
309 KeyBuffer ephemeral_wrapped_ce_key;
310
311 /* Export and install the DE keys */
312 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
313 LOG(ERROR) << "Failed to export de_key";
314 return false;
315 }
316 /* Export and install the CE keys */
317 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
318 LOG(ERROR) << "Failed to export de_key";
319 return false;
320 }
321
322 de_key = std::move(ephemeral_wrapped_de_key);
323 ce_key = std::move(ephemeral_wrapped_ce_key);
324 }
325 if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700326 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700327 s_ce_keys[user_id] = std::move(ce_key);
328
329 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000330 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700331
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000332 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000333 return true;
334}
335
Paul Crowleydf528a72016-03-09 09:31:37 -0800336static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
337 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000338 auto refi = key_map.find(user_id);
339 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700340 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000341 return false;
342 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800343 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000344 return true;
345}
346
Paul Crowley3aa914d2017-10-09 16:35:51 -0700347static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800348 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
349 if (entry == nullptr) {
350 return;
351 }
352 key_ref->contents_mode = entry->file_contents_mode;
353 key_ref->filenames_mode = entry->file_names_mode;
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700354}
355
Paul Crowley3aa914d2017-10-09 16:35:51 -0700356static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700357 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700358 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
359 key_ref.filenames_mode.c_str()) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100360}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100361
Paul Crowleydf528a72016-03-09 09:31:37 -0800362static bool is_numeric(const char* name) {
363 for (const char* p = name; *p != '\0'; p++) {
364 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000365 }
366 return true;
367}
368
369static bool load_all_de_keys() {
370 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800371 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000372 if (!dirp) {
373 PLOG(ERROR) << "Unable to read de key directory";
374 return false;
375 }
376 for (;;) {
377 errno = 0;
378 auto entry = readdir(dirp.get());
379 if (!entry) {
380 if (errno) {
381 PLOG(ERROR) << "Unable to read de key directory";
382 return false;
383 }
384 break;
385 }
386 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
387 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
388 continue;
389 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600390 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000391 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000392 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100393 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800394 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000395 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700396 if (is_wrapped_key_supported()) {
397 KeyBuffer ephemeral_wrapped_key;
398 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
399 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
400 return false;
401 }
402 key = std::move(ephemeral_wrapped_key);
403 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700404 if (!android::vold::installKey(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000405 s_de_key_raw_refs[user_id] = raw_ref;
406 LOG(DEBUG) << "Installed de key for user " << user_id;
407 }
408 }
Eric Biggersa701c452018-10-23 13:06:55 -0700409 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000410 // correct policy set on them, and that no rogue ones exist.
411 return true;
412}
413
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700414bool fscrypt_initialize_systemwide_keys() {
415 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700416 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800417
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700418 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000419 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000420 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800421 }
422
Paul Crowley3aa914d2017-10-09 16:35:51 -0700423 PolicyKeyRef device_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700424 wrapped_key_supported = is_wrapped_key_supported();
425
426 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication,
427 device_key_path, device_key_temp,
428 &device_ref.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700429 return false;
430 get_data_file_encryption_modes(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800431
Paul Crowley3aa914d2017-10-09 16:35:51 -0700432 std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
Eric Biggersa701c452018-10-23 13:06:55 -0700433 std::string mode_filename = std::string("/data") + fscrypt_key_mode;
Tommy Chiua98464f2019-03-26 14:14:19 +0800434 if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700435
Eric Biggersa701c452018-10-23 13:06:55 -0700436 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Tommy Chiua98464f2019-03-26 14:14:19 +0800437 if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700438 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800439
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700440 KeyBuffer per_boot_key;
441 if (!android::vold::randomKey(&per_boot_key)) return false;
442 std::string per_boot_raw_ref;
443 if (!android::vold::installKey(per_boot_key, &per_boot_raw_ref)) return false;
444 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
445 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
446 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
447
Tommy Chiua98464f2019-03-26 14:14:19 +0800448 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700449 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000450 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800451}
452
Eric Biggersa701c452018-10-23 13:06:55 -0700453bool fscrypt_init_user0() {
454 LOG(DEBUG) << "fscrypt_init_user0";
455 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000456 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
457 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
458 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700459 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000460 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000461 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700462 // TODO: switch to loading only DE_0 here once framework makes
463 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000464 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000465 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700466 // We can only safely prepare DE storage here, since CE keys are probably
467 // entangled with user credentials. The framework will always prepare CE
468 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700469 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700470 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000471 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700472 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700473
474 // If this is a non-FBE device that recently left an emulated mode,
475 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700476 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
477 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700478 }
479
Paul Crowley76107cb2016-02-09 10:04:39 +0000480 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000481}
482
Eric Biggersa701c452018-10-23 13:06:55 -0700483bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
484 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
485 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000486 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000487 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000488 // FIXME test for existence of key that is not loaded yet
489 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700490 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800491 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000492 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000493 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800494 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000495 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000496 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000497 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000498 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800499}
500
Eric Biggersce368682019-04-03 15:44:06 -0700501// "Lock" all encrypted directories whose key has been removed. This is needed
502// because merely removing the keyring key doesn't affect inodes in the kernel's
503// inode cache whose per-file key was already set up. So to remove the per-file
504// keys and make the files "appear encrypted", these inodes must be evicted.
505//
506// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
507// objects systemwide. This is overkill, but it's the best available method
508// currently. Don't use drop_caches mode "3" because that also evicts pagecache
509// for in-use files; all files relevant here are already closed and sync'ed.
Pavel Grafovb350ed02017-07-27 17:34:57 +0100510static void drop_caches() {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100511 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700512 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100513 PLOG(ERROR) << "Failed to drop caches during key eviction";
514 }
515}
516
Andrew Scull7ec25c72016-10-31 10:28:25 +0000517static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000518 bool success = true;
519 std::string raw_ref;
520 // If we haven't loaded the CE key, no need to evict it.
521 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700522 success &= android::vold::evictKey(raw_ref);
Pavel Grafovb350ed02017-07-27 17:34:57 +0100523 drop_caches();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000524 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700525
526 if(is_wrapped_key_supported()) {
527 KeyBuffer key;
528 key = s_ce_keys[user_id];
529
530 std::string keystr(key.data(), key.size());
531 Keymaster keymaster;
532
533 if (!keymaster) {
534 s_ce_keys.erase(user_id);
535 s_ce_key_raw_refs.erase(user_id);
536 return false;
537 }
538 keymaster.deleteKey(keystr);
539 }
540
541 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000542 s_ce_key_raw_refs.erase(user_id);
543 return success;
544}
545
Eric Biggersa701c452018-10-23 13:06:55 -0700546bool fscrypt_destroy_user_key(userid_t user_id) {
547 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
548 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000549 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000550 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000551 bool success = true;
552 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000553 success &= evict_ce_key(user_id);
Paul Crowley14c8c072018-09-18 13:30:21 -0700554 success &=
555 lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000556 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000557 auto it = s_ephemeral_users.find(user_id);
558 if (it != s_ephemeral_users.end()) {
559 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000560 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700561 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700562 success &= android::vold::destroyKey(path);
563 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700564 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700565 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700566 success &= android::vold::destroyKey(de_key_path);
567 } else {
568 LOG(INFO) << "Not present so not erasing: " << de_key_path;
569 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100570 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000571 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100572}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800573
Paul Crowley76107cb2016-02-09 10:04:39 +0000574static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700575 if (chmod(path.c_str(), 0000) != 0) {
576 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000577 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700578 }
579#if EMULATED_USES_SELINUX
580 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
581 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000582 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700583 }
584#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000585 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700586}
587
Paul Crowley76107cb2016-02-09 10:04:39 +0000588static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700589 if (chmod(path.c_str(), mode) != 0) {
590 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000591 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700592 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700593 }
594#if EMULATED_USES_SELINUX
595 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
596 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000597 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700598 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700599 }
600#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000601 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700602}
603
Paul Crowley3b71fc52017-10-09 10:55:21 -0700604static bool parse_hex(const std::string& hex, std::string* result) {
605 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800606 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000607 return true;
608 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800609 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800610 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000611 return false;
612 }
613 return true;
614}
615
Paul Crowley3aa914d2017-10-09 16:35:51 -0700616static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
617 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
618}
619
Paul Crowley26a53882017-10-26 11:16:39 -0700620static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
621 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
622}
623
Paul Crowley3aa914d2017-10-09 16:35:51 -0700624static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700625 PolicyKeyRef* key_ref, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700626 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
627 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700628 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700629 if (android::vold::pathExists(secdiscardable_path)) {
630 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
631 return false;
632 } else {
633 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
634 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
635 return false;
636 }
637 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
638 return false;
639 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700640 auto key_path = volkey_path(misc_path, volume_uuid);
641 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
642 PLOG(ERROR) << "Creating directories for: " << key_path;
643 return false;
644 }
Paul Crowley26a53882017-10-26 11:16:39 -0700645 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700646 wrapped_key_supported = is_wrapped_key_supported_external(flags);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700647
Paul Crowley26a53882017-10-26 11:16:39 -0700648 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700649 &key_ref->key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700650 return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700651
652 if (is_ice_supported_external(flags)) {
653 key_ref->contents_mode =
654 android::base::GetProperty("ro.crypto.volume.contents_mode", "ice");
655 } else {
656 key_ref->contents_mode =
657 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
658 }
659
Paul Crowley3aa914d2017-10-09 16:35:51 -0700660 key_ref->filenames_mode =
661 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
662 return true;
663}
664
665static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700666 auto path = volkey_path(misc_path, volume_uuid);
667 if (!android::vold::pathExists(path)) return true;
668 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700669}
670
Eric Biggersa701c452018-10-23 13:06:55 -0700671bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700672 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700673 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700674 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700675 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000676 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700677 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800678 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700679 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700680 auto auth =
681 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700682 auto const directory_path = get_ce_key_directory_path(user_id);
683 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700684
685 KeyBuffer ce_key;
686 if(is_wrapped_key_supported()) {
687 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
688 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
689 LOG(DEBUG) << "Successfully retrieved key";
690 } else {
691 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
692 LOG(DEBUG) << "Successfully retrieved key";
693 }
694 }
695 } else {
696 auto it = s_ce_keys.find(user_id);
697 if (it == s_ce_keys.end()) {
698 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
699 return false;
700 }
701 ce_key = it->second;
702 }
703
Paul Crowleya3630362016-05-17 14:17:56 -0700704 std::string ce_key_path;
705 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700706 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800707 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700708 return true;
709}
710
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800711bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700712 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800713 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700714 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800715 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700716 if (s_ephemeral_users.count(user_id) != 0) return true;
717 std::string token, secret;
718
719 if (!parse_hex(token_hex, &token)) return false;
720 if (!parse_hex(secret_hex, &secret)) return false;
721
722 if (is_wrapped_key_supported()) {
723 auto const directory_path = get_ce_key_directory_path(user_id);
724 auto const paths = get_ce_key_paths(directory_path);
725
726 KeyBuffer ce_key;
727 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
728
729 auto auth = android::vold::KeyAuthentication(token, secret);
730 /* Retrieve key while removing a pin. A secret is needed */
731 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
732 LOG(DEBUG) << "Successfully retrieved key";
733 } else {
734 /* Retrieve key when going None to swipe and vice versa when a
735 synthetic password is present */
736 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
737 LOG(DEBUG) << "Successfully retrieved key";
738 }
739 }
740
741 std::string ce_key_path;
742 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
743 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
744 return false;
745 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800746 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700747 }
748 return true;
749}
750
Eric Biggersa701c452018-10-23 13:06:55 -0700751bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
752 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
753 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700754 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700755 auto const directory_path = get_ce_key_directory_path(user_id);
756 auto const paths = get_ce_key_paths(directory_path);
757 if (paths.empty()) {
758 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
759 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000760 }
Paul Crowleya3630362016-05-17 14:17:56 -0700761 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000762 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000763}
764
Jeff Sharkey47695b22016-02-01 17:02:29 -0700765// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700766bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700767 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700768 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700769 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700770 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000771 if (s_ce_key_raw_refs.count(user_id) != 0) {
772 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000773 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000774 }
775 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800776 if (!parse_hex(token_hex, &token)) return false;
777 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000778 android::vold::KeyAuthentication auth(token, secret);
779 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000780 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000781 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800782 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700783 } else {
784 // When in emulation mode, we just use chmod. However, we also
785 // unlock directories when not in emulation mode, to bring devices
786 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000787 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800788 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700789 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
790 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700791 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000792 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700793 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800794 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000795 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800796}
797
Jeff Sharkey47695b22016-02-01 17:02:29 -0700798// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700799bool fscrypt_lock_user_key(userid_t user_id) {
800 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
801 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000802 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700803 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800804 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000805 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800806 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700807 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
808 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000809 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000810 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800811 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800812 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700813
Paul Crowley76107cb2016-02-09 10:04:39 +0000814 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800815}
816
Paul Crowley82b41ff2017-10-20 08:17:54 -0700817static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
818 userid_t user_id, int flags) {
819 if (0 != android::vold::ForkExecvp(
820 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
821 std::to_string(user_id), std::to_string(flags)})) {
822 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700823 return false;
824 }
825 return true;
826}
827
Eric Biggersa701c452018-10-23 13:06:55 -0700828bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700829 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700830 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800831 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700832
Paul Crowley82b41ff2017-10-20 08:17:54 -0700833 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600834 // DE_sys key
835 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
836 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
837 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600838
839 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700840 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
841 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800842 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700843 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
844
Paul Crowley3b71fc52017-10-09 10:55:21 -0700845 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700846 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600847#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700848 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700849 multiuser_get_uid(user_id, AID_EVERYBODY)))
850 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600851#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700852 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600853
Paul Crowley6b756ce2017-10-05 14:07:09 -0700854 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
855 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800856 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700857 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000858 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000859
Eric Biggersa701c452018-10-23 13:06:55 -0700860 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700861 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700862 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700863 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
864 get_data_file_encryption_modes(&de_ref);
865 if (!ensure_policy(de_ref, system_de_path)) return false;
866 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800867 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700868 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700869 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700870 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700871 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700872 }
Paul Crowley285956f2016-01-20 13:12:38 +0000873 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800874
Paul Crowley82b41ff2017-10-20 08:17:54 -0700875 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600876 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700877 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
878 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800879 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600880 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
881 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800882
Paul Crowley3b71fc52017-10-09 10:55:21 -0700883 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700884 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
885 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800886 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700887 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000888 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
889 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700890
Eric Biggersa701c452018-10-23 13:06:55 -0700891 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700892 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700893 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700894 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
895 get_data_file_encryption_modes(&ce_ref);
896 if (!ensure_policy(ce_ref, system_ce_path)) return false;
897 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800898 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700899
Paul Crowley3aa914d2017-10-09 16:35:51 -0700900 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700901 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700902 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700903 if (!ensure_policy(ce_ref, media_ce_path)) return false;
904 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700905 }
Paul Crowley1a965262017-10-13 13:49:50 -0700906
907 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700908 // Now that credentials have been installed, we can run restorecon
909 // over these paths
910 // NOTE: these paths need to be kept in sync with libselinux
911 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700912 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700913 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700914 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800915 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700916 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800917
Paul Crowley76107cb2016-02-09 10:04:39 +0000918 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800919}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600920
Eric Biggersa701c452018-10-23 13:06:55 -0700921bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
922 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600923 << ", user " << user_id << ", flags " << flags;
924 bool res = true;
925
Paul Crowley82b41ff2017-10-20 08:17:54 -0700926 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
927
928 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700929 // CE_n key
930 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
931 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800932 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700933 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
934 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
935
936 res &= destroy_dir(media_ce_path);
937 res &= destroy_dir(user_ce_path);
938 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700939 res &= destroy_dir(system_ce_path);
940 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800941 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700942 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700943 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700944 res &= destroy_volkey(misc_ce_path, volume_uuid);
945 }
Paul Crowley8e550662017-10-16 17:01:44 -0700946 }
947 }
948
Paul Crowley82b41ff2017-10-20 08:17:54 -0700949 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600950 // DE_sys key
951 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
952 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
953 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600954
955 // DE_n key
956 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
957 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800958 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600959 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
960
Paul Crowley8e550662017-10-16 17:01:44 -0700961 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700962 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600963 res &= destroy_dir(system_legacy_path);
964#if MANAGE_MISC_DIRS
965 res &= destroy_dir(misc_legacy_path);
966#endif
967 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600968 res &= destroy_dir(system_de_path);
969 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800970 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700971 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700972 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700973 res &= destroy_volkey(misc_de_path, volume_uuid);
974 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600975 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600976 }
977
978 return res;
979}
Rubin Xu2436e272017-04-27 20:43:10 +0100980
Paul Crowleyc6433a22017-10-24 14:54:43 -0700981static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
982 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
983 if (!dirp) {
984 PLOG(ERROR) << "Unable to open directory: " + directory_path;
985 return false;
986 }
987 bool res = true;
988 for (;;) {
989 errno = 0;
990 auto const entry = readdir(dirp.get());
991 if (!entry) {
992 if (errno) {
993 PLOG(ERROR) << "Unable to read directory: " + directory_path;
994 return false;
995 }
996 break;
997 }
998 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
999 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
1000 continue;
1001 }
1002 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
1003 }
1004 return res;
1005}
1006
Eric Biggersa701c452018-10-23 13:06:55 -07001007bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -07001008 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -07001009 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -07001010 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
1011 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -07001012 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
1013 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
1014 return res;
1015}