blob: 0020cf27e52f86c65de2013962c1fab64b48b138 [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>
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -070028#include <optional>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000029#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070030#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080031#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070032#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000033
Paul Crowleydf528a72016-03-09 09:31:37 -080034#include <dirent.h>
35#include <errno.h>
36#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070037#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070038#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080039#include <sys/mount.h>
40#include <sys/stat.h>
41#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070042#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000043
Paul Crowley480fcd22015-08-24 14:53:28 +010044#include <private/android_filesystem_config.h>
45
Paul Crowley82b41ff2017-10-20 08:17:54 -070046#include "android/os/IVold.h"
47
Paul Lawrence731a7a22015-04-28 22:14:15 +000048#include "cryptfs.h"
49
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070050#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060051#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070052
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080053#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070054#include <cutils/properties.h>
55
Eric Biggersa701c452018-10-23 13:06:55 -070056#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070057#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080058
Elliott Hughes7e128fb2015-12-04 15:50:53 -080059#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080060#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070061#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080062#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070063#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080064#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000065
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080066using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080067using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000068using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010069using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070070using android::vold::Keymaster;
71using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080072using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070073using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080074
Paul Lawrence731a7a22015-04-28 22:14:15 +000075namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000076
Eric Biggersa701c452018-10-23 13:06:55 -070077const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080078const std::string device_key_path = device_key_dir + "/key";
79const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080080
Paul Crowleydf528a72016-03-09 09:31:37 -080081const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
82const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070083const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000084
Paul Crowley26a53882017-10-26 11:16:39 -070085const std::string systemwide_volume_key_dir =
86 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
87
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070088bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080089
Paul Crowleydf528a72016-03-09 09:31:37 -080090// Some users are ephemeral, don't try to wipe their keys from disk
91std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080092
Paul Crowleydf528a72016-03-09 09:31:37 -080093// Map user ids to key references
94std::map<userid_t, std::string> s_de_key_raw_refs;
95std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Lawrence731a7a22015-04-28 22:14:15 +000096
Paul Crowley14c8c072018-09-18 13:30:21 -070097} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000098
Eric Biggersa701c452018-10-23 13:06:55 -070099static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000100 return property_get_bool("persist.sys.emulate_fbe", false);
101}
102
Paul Crowley3b71fc52017-10-09 10:55:21 -0700103static const char* escape_empty(const std::string& value) {
104 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000105}
106
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000107static std::string get_de_key_path(userid_t user_id) {
108 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
109}
110
Paul Crowleya3630362016-05-17 14:17:56 -0700111static std::string get_ce_key_directory_path(userid_t user_id) {
112 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
113}
114
115// Returns the keys newest first
116static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
117 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
118 if (!dirp) {
119 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
120 return std::vector<std::string>();
121 }
122 std::vector<std::string> result;
123 for (;;) {
124 errno = 0;
125 auto const entry = readdir(dirp.get());
126 if (!entry) {
127 if (errno) {
128 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
129 return std::vector<std::string>();
130 }
131 break;
132 }
133 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
134 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
135 continue;
136 }
137 result.emplace_back(directory_path + "/" + entry->d_name);
138 }
139 std::sort(result.begin(), result.end());
140 std::reverse(result.begin(), result.end());
141 return result;
142}
143
144static std::string get_ce_key_current_path(const std::string& directory_path) {
145 return directory_path + "/current";
146}
147
148static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700149 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700150 if (paths.empty()) {
151 *ce_key_path = get_ce_key_current_path(directory_path);
152 return true;
153 }
154 for (unsigned int i = 0; i < UINT_MAX; i++) {
155 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
156 if (paths[0] < candidate) {
157 *ce_key_path = candidate;
158 return true;
159 }
160 }
161 return false;
162}
163
164// Discard all keys but the named one; rename it to canonical name.
165// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700166static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700167 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700169 if (other_path != to_fix) {
170 android::vold::destroyKey(other_path);
171 }
172 }
173 auto const current_path = get_ce_key_current_path(directory_path);
174 if (to_fix != current_path) {
175 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
176 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
177 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800178 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700179 }
180 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800181 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700182}
183
184static bool read_and_fixate_user_ce_key(userid_t user_id,
185 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700187 auto const directory_path = get_ce_key_directory_path(user_id);
188 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
191 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
192 LOG(DEBUG) << "Successfully retrieved key";
193 fixate_user_ce_key(directory_path, ce_key_path, paths);
194 return true;
195 }
196 }
197 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
198 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100199}
200
Eric Biggers83a73d72019-09-30 13:06:47 -0700201// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700202static void get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700203 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
204 if (entry == nullptr) {
205 return;
206 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700207 if (!ParseOptions(entry->encryption_options, options)) {
208 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
209 << entry->encryption_options;
210 return;
211 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700212}
213
214// Retrieve the version to use for encryption policies on the /data filesystem.
215static int get_data_file_policy_version(void) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700216 EncryptionOptions options;
217 get_data_file_encryption_options(&options);
218 return options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700219}
220
221// Retrieve the options to use for encryption policies on adoptable storage.
Steven Laverd4eb3412019-11-09 19:05:21 -0800222static bool get_volume_file_encryption_options(EncryptionOptions* options, int flags) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700223 auto contents_mode =
Steven Laverd4eb3412019-11-09 19:05:21 -0800224 android::base::GetProperty("ro.crypto.volume.contents_mode",
225 is_ice_supported_external(flags) ? "ice" : "aes-256-xts");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700226 auto filenames_mode =
Eric Biggers83a73d72019-09-30 13:06:47 -0700227 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700228 return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
229 contents_mode + ":" + filenames_mode + ":v1"),
230 options);
Eric Biggers83a73d72019-09-30 13:06:47 -0700231}
232
Eric Biggersf3dc4202019-09-30 13:05:58 -0700233// Install a key for use by encrypted files on the /data filesystem.
234static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700235 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700236}
237
238// Evict a key for use by encrypted files on the /data filesystem.
239static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700240 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700241}
242
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700243bool is_ice_supported_external(int flags) {
244 /*
245 * Logic can be changed when more card controllers start supporting ICE.
246 * Until then, checking only for UFS card.
247 */
248 if ((flags & android::vold::Disk::Flags::kUfsCard) ==
249 android::vold::Disk::Flags::kUfsCard)
250 return true;
251 return false;
252}
253
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700254bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800255 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700256}
257
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700258bool is_wrapped_key_supported_external(int flags) {
259 if (is_ice_supported_external(flags))
260 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700261 return false;
262}
263
Neeraj Sonic480f912018-12-14 15:18:15 +0530264bool is_metadata_wrapped_key_supported() {
265 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
266}
267
Paul Crowleydf528a72016-03-09 09:31:37 -0800268static bool read_and_install_user_ce_key(userid_t user_id,
269 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000270 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100271 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700272 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000273 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700274
275 if (is_wrapped_key_supported()) {
276 KeyBuffer ephemeral_wrapped_key;
277 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
278 LOG(ERROR) << "Failed to export ce key";
279 return false;
280 }
281 ce_key = std::move(ephemeral_wrapped_key);
282 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700283 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000284 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000285 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000286 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000287}
288
Paul Crowleydf528a72016-03-09 09:31:37 -0800289static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000290 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000291 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
292 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000293 return false;
294 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000295 return true;
296}
297
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600298static bool destroy_dir(const std::string& dir) {
299 LOG(DEBUG) << "Destroying: " << dir;
300 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
301 PLOG(ERROR) << "Failed to destroy " << dir;
302 return false;
303 }
304 return true;
305}
306
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000307// NB this assumes that there is only one thread listening for crypt commands, because
308// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000309static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100310 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700311
312 if(is_wrapped_key_supported()) {
313 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
314 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
315 } else {
316 if (!android::vold::randomKey(&de_key)) return false;
317 if (!android::vold::randomKey(&ce_key)) return false;
318 }
319
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000320 if (create_ephemeral) {
321 // If the key should be created as ephemeral, don't store it.
322 s_ephemeral_users.insert(user_id);
323 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700324 auto const directory_path = get_ce_key_directory_path(user_id);
325 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
326 auto const paths = get_ce_key_paths(directory_path);
327 std::string ce_key_path;
328 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700329 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
330 ce_key))
331 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700332 fixate_user_ce_key(directory_path, ce_key_path, paths);
333 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700334 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700335 kEmptyAuthentication, de_key))
336 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100337 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700338
339 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000340 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000341 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700342
343 if (is_wrapped_key_supported()) {
344 KeyBuffer ephemeral_wrapped_de_key;
345 KeyBuffer ephemeral_wrapped_ce_key;
346
347 /* Export and install the DE keys */
348 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
349 LOG(ERROR) << "Failed to export de_key";
350 return false;
351 }
352 /* Export and install the CE keys */
353 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
354 LOG(ERROR) << "Failed to export de_key";
355 return false;
356 }
357
358 de_key = std::move(ephemeral_wrapped_de_key);
359 ce_key = std::move(ephemeral_wrapped_ce_key);
360 }
Steven Lavered747be2019-10-24 16:40:01 -0700361 if (!install_data_key(de_key, &de_raw_ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700362 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700363
364 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000365 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700366
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000367 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000368 return true;
369}
370
Paul Crowleydf528a72016-03-09 09:31:37 -0800371static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
372 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000373 auto refi = key_map.find(user_id);
374 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700375 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000376 return false;
377 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800378 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000379 return true;
380}
381
Paul Crowleydf528a72016-03-09 09:31:37 -0800382static bool is_numeric(const char* name) {
383 for (const char* p = name; *p != '\0'; p++) {
384 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000385 }
386 return true;
387}
388
389static bool load_all_de_keys() {
390 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800391 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000392 if (!dirp) {
393 PLOG(ERROR) << "Unable to read de key directory";
394 return false;
395 }
396 for (;;) {
397 errno = 0;
398 auto entry = readdir(dirp.get());
399 if (!entry) {
400 if (errno) {
401 PLOG(ERROR) << "Unable to read de key directory";
402 return false;
403 }
404 break;
405 }
406 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
407 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
408 continue;
409 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600410 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000411 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000412 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100413 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800414 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000415 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700416 if (is_wrapped_key_supported()) {
417 KeyBuffer ephemeral_wrapped_key;
418 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
419 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
420 return false;
421 }
422 key = std::move(ephemeral_wrapped_key);
423 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700424 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000425 s_de_key_raw_refs[user_id] = raw_ref;
426 LOG(DEBUG) << "Installed de key for user " << user_id;
427 }
428 }
Eric Biggersa701c452018-10-23 13:06:55 -0700429 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000430 // correct policy set on them, and that no rogue ones exist.
431 return true;
432}
433
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700434bool fscrypt_initialize_systemwide_keys() {
435 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700436 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800437
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700438 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000439 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000440 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800441 }
442
Paul Crowley5e53ff62019-10-24 14:55:17 -0700443 EncryptionPolicy device_policy;
444 get_data_file_encryption_options(&device_policy.options);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700445 wrapped_key_supported = is_wrapped_key_supported();
446
Eric Biggers83a73d72019-09-30 13:06:47 -0700447 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700448 device_key_temp, "", device_policy.options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800449 &device_policy.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700450 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800451
Paul Crowley5e53ff62019-10-24 14:55:17 -0700452 std::string options_string;
453 if (!OptionsToString(device_policy.options, &options_string)) {
454 LOG(ERROR) << "Unable to serialize options";
455 return false;
456 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700457 std::string options_filename = std::string("/data") + fscrypt_key_mode;
458 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700459
Eric Biggersa701c452018-10-23 13:06:55 -0700460 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700461 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700462 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800463
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700464 KeyBuffer per_boot_key;
465 if (!android::vold::randomKey(&per_boot_key)) return false;
466 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700467 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700468 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
469 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
470 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
471
Tommy Chiua98464f2019-03-26 14:14:19 +0800472 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700473 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000474 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800475}
476
Eric Biggersa701c452018-10-23 13:06:55 -0700477bool fscrypt_init_user0() {
478 LOG(DEBUG) << "fscrypt_init_user0";
479 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000480 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
481 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
482 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700483 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000484 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000485 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700486 // TODO: switch to loading only DE_0 here once framework makes
487 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000488 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000489 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700490 // We can only safely prepare DE storage here, since CE keys are probably
491 // entangled with user credentials. The framework will always prepare CE
492 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700493 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700494 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000495 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700496 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700497
498 // If this is a non-FBE device that recently left an emulated mode,
499 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700500 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
501 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700502 }
503
Paul Crowley76107cb2016-02-09 10:04:39 +0000504 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000505}
506
Eric Biggersa701c452018-10-23 13:06:55 -0700507bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
508 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
509 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000510 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000511 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000512 // FIXME test for existence of key that is not loaded yet
513 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700514 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800515 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000516 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000517 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800518 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000519 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000520 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000521 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000522 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800523}
524
Eric Biggersce368682019-04-03 15:44:06 -0700525// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700526// in the case where the keys are being put in the session keyring (rather in
527// the newer filesystem-level keyrings), because removing a key from the session
528// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
529// was already set up. So to remove the per-file keys and make the files
530// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700531//
532// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
533// objects systemwide. This is overkill, but it's the best available method
534// currently. Don't use drop_caches mode "3" because that also evicts pagecache
535// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700536static void drop_caches_if_needed() {
537 if (android::vold::isFsKeyringSupported()) {
538 return;
539 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100540 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700541 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100542 PLOG(ERROR) << "Failed to drop caches during key eviction";
543 }
544}
545
Andrew Scull7ec25c72016-10-31 10:28:25 +0000546static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000547 bool success = true;
548 std::string raw_ref;
549 // If we haven't loaded the CE key, no need to evict it.
550 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700551 success &= evict_data_key(raw_ref);
552 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000553 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700554
Andrew Scull7ec25c72016-10-31 10:28:25 +0000555 s_ce_key_raw_refs.erase(user_id);
556 return success;
557}
558
Eric Biggersa701c452018-10-23 13:06:55 -0700559bool fscrypt_destroy_user_key(userid_t user_id) {
560 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
561 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000562 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000563 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000564 bool success = true;
565 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000566 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700567 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 +0000568 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000569 auto it = s_ephemeral_users.find(user_id);
570 if (it != s_ephemeral_users.end()) {
571 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000572 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700573 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700574 success &= android::vold::destroyKey(path);
575 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700576 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700577 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700578 success &= android::vold::destroyKey(de_key_path);
579 } else {
580 LOG(INFO) << "Not present so not erasing: " << de_key_path;
581 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100582 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000583 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100584}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800585
Paul Crowley76107cb2016-02-09 10:04:39 +0000586static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700587 if (chmod(path.c_str(), 0000) != 0) {
588 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000589 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700590 }
591#if EMULATED_USES_SELINUX
592 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
593 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000594 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700595 }
596#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000597 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700598}
599
Paul Crowley76107cb2016-02-09 10:04:39 +0000600static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700601 if (chmod(path.c_str(), mode) != 0) {
602 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000603 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700604 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700605 }
606#if EMULATED_USES_SELINUX
607 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
608 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000609 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700610 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700611 }
612#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000613 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700614}
615
Paul Crowley3b71fc52017-10-09 10:55:21 -0700616static bool parse_hex(const std::string& hex, std::string* result) {
617 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800618 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000619 return true;
620 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800621 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800622 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000623 return false;
624 }
625 return true;
626}
627
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700628static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
629 const std::string& token_hex, const std::string& secret_hex) {
630 std::string token, secret;
631 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
632 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
633 if (secret.empty()) {
634 return kEmptyAuthentication;
635 } else {
636 return android::vold::KeyAuthentication(token, secret);
637 }
638}
639
Paul Crowley3aa914d2017-10-09 16:35:51 -0700640static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
641 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
642}
643
Paul Crowley26a53882017-10-26 11:16:39 -0700644static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
645 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
646}
647
Paul Crowley3aa914d2017-10-09 16:35:51 -0700648static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800649 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700650 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
651 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700652 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700653 if (android::vold::pathExists(secdiscardable_path)) {
654 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
655 return false;
656 } else {
657 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
658 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
659 return false;
660 }
661 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
662 return false;
663 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700664 auto key_path = volkey_path(misc_path, volume_uuid);
665 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
666 PLOG(ERROR) << "Creating directories for: " << key_path;
667 return false;
668 }
Paul Crowley26a53882017-10-26 11:16:39 -0700669 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700670
Steven Laverd4eb3412019-11-09 19:05:21 -0800671 if (!get_volume_file_encryption_options(&policy->options, flags)) return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700672 wrapped_key_supported = is_wrapped_key_supported_external(flags);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700673
Eric Biggers83a73d72019-09-30 13:06:47 -0700674 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley5e53ff62019-10-24 14:55:17 -0700675 volume_uuid, policy->options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800676 &policy->key_raw_ref, wrapped_key_supported);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700677}
678
679static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700680 auto path = volkey_path(misc_path, volume_uuid);
681 if (!android::vold::pathExists(path)) return true;
682 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700683}
684
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700685static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
686 const android::vold::KeyAuthentication& retrieve_auth,
687 const android::vold::KeyAuthentication& store_auth) {
688 if (s_ephemeral_users.count(user_id) != 0) return true;
689 auto const directory_path = get_ce_key_directory_path(user_id);
690 KeyBuffer ce_key;
691 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
692 if (android::vold::retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
693 LOG(DEBUG) << "Successfully retrieved key";
694 // TODO(147732812): Remove this once Locksettingservice is fixed.
695 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
696 // changed from swipe to none or vice-versa
697 } else if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
698 LOG(DEBUG) << "Successfully retrieved key with empty auth";
699 } else {
700 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
701 return false;
702 }
703 auto const paths = get_ce_key_paths(directory_path);
Steven Laver4771d982020-01-23 17:03:42 -0800704
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700705 std::string ce_key_path;
706 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
707 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
708 return false;
709 if (!android::vold::FsyncDirectory(directory_path)) return false;
710 return true;
711}
712
Eric Biggersa701c452018-10-23 13:06:55 -0700713bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700714 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700715 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700716 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700717 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700718 auto auth = authentication_from_hex(token_hex, secret_hex);
719 if (!auth) return false;
720 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
Paul Crowleya3630362016-05-17 14:17:56 -0700721}
722
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800723bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700724 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800725 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700726 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800727 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700728 auto auth = authentication_from_hex(token_hex, secret_hex);
729 if (!auth) return false;
730 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700731}
732
Eric Biggersa701c452018-10-23 13:06:55 -0700733bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
734 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
735 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700736 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700737 auto const directory_path = get_ce_key_directory_path(user_id);
738 auto const paths = get_ce_key_paths(directory_path);
739 if (paths.empty()) {
740 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
741 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000742 }
Paul Crowleya3630362016-05-17 14:17:56 -0700743 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000744 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000745}
746
Jeff Sharkey47695b22016-02-01 17:02:29 -0700747// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700748bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700749 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700750 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700751 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700752 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000753 if (s_ce_key_raw_refs.count(user_id) != 0) {
754 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000755 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000756 }
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700757 auto auth = authentication_from_hex(token_hex, secret_hex);
758 if (!auth) return false;
759 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000760 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000761 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800762 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700763 } else {
764 // When in emulation mode, we just use chmod. However, we also
765 // unlock directories when not in emulation mode, to bring devices
766 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000767 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800768 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700769 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
770 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700771 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000772 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700773 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800774 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000775 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800776}
777
Jeff Sharkey47695b22016-02-01 17:02:29 -0700778// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700779bool fscrypt_lock_user_key(userid_t user_id) {
780 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
781 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000782 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700783 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800784 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000785 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800786 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700787 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
788 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000789 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000790 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800791 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800792 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700793
Paul Crowley76107cb2016-02-09 10:04:39 +0000794 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800795}
796
Paul Crowley82b41ff2017-10-20 08:17:54 -0700797static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
798 userid_t user_id, int flags) {
799 if (0 != android::vold::ForkExecvp(
800 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
801 std::to_string(user_id), std::to_string(flags)})) {
802 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700803 return false;
804 }
805 return true;
806}
807
Eric Biggersa701c452018-10-23 13:06:55 -0700808bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700809 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700810 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800811 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700812
Paul Crowley82b41ff2017-10-20 08:17:54 -0700813 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600814 // DE_sys key
815 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
816 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
817 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600818
819 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700820 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
821 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800822 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700823 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
824
Paul Crowley3b71fc52017-10-09 10:55:21 -0700825 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700826 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600827#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700828 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700829 multiuser_get_uid(user_id, AID_EVERYBODY)))
830 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600831#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700832 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600833
Paul Crowley6b756ce2017-10-05 14:07:09 -0700834 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
835 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800836 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700837 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000838 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000839
Eric Biggersa701c452018-10-23 13:06:55 -0700840 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700841 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700842 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700843 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
844 return false;
845 get_data_file_encryption_options(&de_policy.options);
846 if (!EnsurePolicy(de_policy, system_de_path)) return false;
847 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
848 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700849 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800850 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700851 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700852 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700853 }
Paul Crowley285956f2016-01-20 13:12:38 +0000854 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800855
Paul Crowley82b41ff2017-10-20 08:17:54 -0700856 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600857 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700858 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
859 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800860 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600861 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
862 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800863
Paul Crowley3b71fc52017-10-09 10:55:21 -0700864 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700865 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
866 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800867 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700868 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000869 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
870 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700871
Eric Biggersa701c452018-10-23 13:06:55 -0700872 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700873 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700874 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700875 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
876 return false;
877 get_data_file_encryption_options(&ce_policy.options);
878 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
879 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
880 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700881
Paul Crowley3aa914d2017-10-09 16:35:51 -0700882 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800883 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700884 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700885 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
886 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700887 }
Paul Crowley1a965262017-10-13 13:49:50 -0700888
889 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700890 // Now that credentials have been installed, we can run restorecon
891 // over these paths
892 // NOTE: these paths need to be kept in sync with libselinux
893 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700894 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700895 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700896 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800897 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700898 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800899
Paul Crowley76107cb2016-02-09 10:04:39 +0000900 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800901}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600902
Eric Biggersa701c452018-10-23 13:06:55 -0700903bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
904 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600905 << ", user " << user_id << ", flags " << flags;
906 bool res = true;
907
Paul Crowley82b41ff2017-10-20 08:17:54 -0700908 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
909
910 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700911 // CE_n key
912 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
913 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800914 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700915 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
916 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
917
918 res &= destroy_dir(media_ce_path);
919 res &= destroy_dir(user_ce_path);
920 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700921 res &= destroy_dir(system_ce_path);
922 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800923 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700924 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700925 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700926 res &= destroy_volkey(misc_ce_path, volume_uuid);
927 }
Paul Crowley8e550662017-10-16 17:01:44 -0700928 }
929 }
930
Paul Crowley82b41ff2017-10-20 08:17:54 -0700931 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600932 // DE_sys key
933 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
934 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
935 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600936
937 // DE_n key
938 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
939 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800940 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600941 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
942
Paul Crowley8e550662017-10-16 17:01:44 -0700943 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700944 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600945 res &= destroy_dir(system_legacy_path);
946#if MANAGE_MISC_DIRS
947 res &= destroy_dir(misc_legacy_path);
948#endif
949 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600950 res &= destroy_dir(system_de_path);
951 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800952 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700953 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700954 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700955 res &= destroy_volkey(misc_de_path, volume_uuid);
956 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600957 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600958 }
959
960 return res;
961}
Rubin Xu2436e272017-04-27 20:43:10 +0100962
Paul Crowleyc6433a22017-10-24 14:54:43 -0700963static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
964 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
965 if (!dirp) {
966 PLOG(ERROR) << "Unable to open directory: " + directory_path;
967 return false;
968 }
969 bool res = true;
970 for (;;) {
971 errno = 0;
972 auto const entry = readdir(dirp.get());
973 if (!entry) {
974 if (errno) {
975 PLOG(ERROR) << "Unable to read directory: " + directory_path;
976 return false;
977 }
978 break;
979 }
980 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
981 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
982 continue;
983 }
984 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
985 }
986 return res;
987}
988
Eric Biggersa701c452018-10-23 13:06:55 -0700989bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700990 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700991 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700992 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
993 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700994 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
995 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
996 return res;
997}