blob: 82a2012ce99e2b558fd6e889aefbf39181667fda [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
Paul Crowley1ef25582016-01-21 20:26:12 +000019#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070020#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080021#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070022#include "VoldUtil.h"
23
Paul Crowleya3630362016-05-17 14:17:56 -070024#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000025#include <map>
Barani Muthukumaranb1927c22019-10-31 22:59:34 -070026#include <optional>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000027#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070028#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080029#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070030#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000031
Paul Crowleydf528a72016-03-09 09:31:37 -080032#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070035#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070036#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080037#include <sys/mount.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070040#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000041
Paul Crowley480fcd22015-08-24 14:53:28 +010042#include <private/android_filesystem_config.h>
Martijn Coenenaee40512020-02-18 16:29:25 +010043#include <private/android_projectid_config.h>
Paul Crowley480fcd22015-08-24 14:53:28 +010044
Paul Crowley82b41ff2017-10-20 08:17:54 -070045#include "android/os/IVold.h"
46
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070047#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060048#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080050#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070051#include <cutils/properties.h>
52
Eric Biggersa701c452018-10-23 13:06:55 -070053#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070054#include <keyutils.h>
Eric Biggerseb566d02020-07-06 13:46:38 -070055#include <libdm/dm.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080056
Elliott Hughes7e128fb2015-12-04 15:50:53 -080057#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080058#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070059#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080060#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070061#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080062#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000063
Eric Biggerseb566d02020-07-06 13:46:38 -070064using android::base::Basename;
65using android::base::Realpath;
66using android::base::StartsWith;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080067using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080068using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley77df7f22020-01-23 15:29:30 -080069using android::vold::BuildDataPath;
Eric Biggers6b840392020-11-02 15:11:06 -080070using android::vold::IsDotOrDotDot;
Martijn Coenenfd9cdbf2020-02-11 14:20:29 +010071using android::vold::IsFilesystemSupported;
Paul Crowley05720802016-02-08 15:55:41 +000072using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010073using android::vold::KeyBuffer;
Paul Crowley249c2fb2020-02-07 12:51:56 -080074using android::vold::KeyGeneration;
Paul Crowley4eac2642020-02-12 11:04:05 -080075using android::vold::retrieveKey;
76using android::vold::retrieveOrGenerateKey;
Martijn Coenenfd9cdbf2020-02-11 14:20:29 +010077using android::vold::SetQuotaInherit;
78using android::vold::SetQuotaProjectId;
Tommy Chiua98464f2019-03-26 14:14:19 +080079using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070080using namespace android::fscrypt;
Eric Biggerseb566d02020-07-06 13:46:38 -070081using namespace android::dm;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080082
Paul Lawrence731a7a22015-04-28 22:14:15 +000083namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000084
Eric Biggersa701c452018-10-23 13:06:55 -070085const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080086const std::string device_key_path = device_key_dir + "/key";
87const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080088
Paul Crowleydf528a72016-03-09 09:31:37 -080089const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
90const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070091const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000092
Paul Crowley26a53882017-10-26 11:16:39 -070093const std::string systemwide_volume_key_dir =
94 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
95
Paul Crowleydf528a72016-03-09 09:31:37 -080096// Some users are ephemeral, don't try to wipe their keys from disk
97std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080098
Paul Crowley77df7f22020-01-23 15:29:30 -080099// Map user ids to encryption policies
100std::map<userid_t, EncryptionPolicy> s_de_policies;
101std::map<userid_t, EncryptionPolicy> s_ce_policies;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000102
Paul Crowley14c8c072018-09-18 13:30:21 -0700103} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +0000104
Paul Crowley249c2fb2020-02-07 12:51:56 -0800105// Returns KeyGeneration suitable for key as described in EncryptionOptions
106static KeyGeneration makeGen(const EncryptionOptions& options) {
107 return KeyGeneration{FSCRYPT_MAX_KEY_SIZE, true, options.use_hw_wrapped_key};
108}
109
Eric Biggersa701c452018-10-23 13:06:55 -0700110static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000111 return property_get_bool("persist.sys.emulate_fbe", false);
112}
113
Paul Crowley3b71fc52017-10-09 10:55:21 -0700114static const char* escape_empty(const std::string& value) {
115 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000116}
117
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000118static std::string get_de_key_path(userid_t user_id) {
119 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
120}
121
Paul Crowleya3630362016-05-17 14:17:56 -0700122static std::string get_ce_key_directory_path(userid_t user_id) {
123 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
124}
125
126// Returns the keys newest first
127static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
128 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
129 if (!dirp) {
130 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
131 return std::vector<std::string>();
132 }
133 std::vector<std::string> result;
134 for (;;) {
135 errno = 0;
136 auto const entry = readdir(dirp.get());
137 if (!entry) {
138 if (errno) {
139 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
140 return std::vector<std::string>();
141 }
142 break;
143 }
Eric Biggers6b840392020-11-02 15:11:06 -0800144 if (IsDotOrDotDot(*entry)) continue;
Paul Crowleya3630362016-05-17 14:17:56 -0700145 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
146 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
147 continue;
148 }
149 result.emplace_back(directory_path + "/" + entry->d_name);
150 }
151 std::sort(result.begin(), result.end());
152 std::reverse(result.begin(), result.end());
153 return result;
154}
155
156static std::string get_ce_key_current_path(const std::string& directory_path) {
157 return directory_path + "/current";
158}
159
160static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700161 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700162 if (paths.empty()) {
163 *ce_key_path = get_ce_key_current_path(directory_path);
164 return true;
165 }
166 for (unsigned int i = 0; i < UINT_MAX; i++) {
167 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
168 if (paths[0] < candidate) {
169 *ce_key_path = candidate;
170 return true;
171 }
172 }
173 return false;
174}
175
176// Discard all keys but the named one; rename it to canonical name.
177// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700178static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700179 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700180 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700181 if (other_path != to_fix) {
182 android::vold::destroyKey(other_path);
183 }
184 }
185 auto const current_path = get_ce_key_current_path(directory_path);
186 if (to_fix != current_path) {
187 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
188 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
189 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800190 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700191 }
192 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800193 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700194}
195
196static bool read_and_fixate_user_ce_key(userid_t user_id,
197 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700198 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700199 auto const directory_path = get_ce_key_directory_path(user_id);
200 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700201 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700202 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
Eric Biggersf74373b2020-11-05 19:58:26 -0800203 if (retrieveKey(ce_key_path, auth, ce_key)) {
Paul Crowleya3630362016-05-17 14:17:56 -0700204 LOG(DEBUG) << "Successfully retrieved key";
205 fixate_user_ce_key(directory_path, ce_key_path, paths);
206 return true;
207 }
208 }
209 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
210 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100211}
212
Eric Biggerseb566d02020-07-06 13:46:38 -0700213static bool IsEmmcStorage(const std::string& blk_device) {
214 // Handle symlinks.
215 std::string real_path;
216 if (!Realpath(blk_device, &real_path)) {
217 real_path = blk_device;
218 }
219
220 // Handle logical volumes.
221 auto& dm = DeviceMapper::Instance();
222 for (;;) {
223 auto parent = dm.GetParentBlockDeviceByPath(real_path);
224 if (!parent.has_value()) break;
225 real_path = *parent;
226 }
227
228 // Now we should have the "real" block device.
229 LOG(DEBUG) << "IsEmmcStorage(): blk_device = " << blk_device << ", real_path=" << real_path;
230 return StartsWith(Basename(real_path), "mmcblk");
231}
232
Eric Biggers83a73d72019-09-30 13:06:47 -0700233// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley77df7f22020-01-23 15:29:30 -0800234static bool get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700235 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
236 if (entry == nullptr) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800237 LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
238 return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700239 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700240 if (!ParseOptions(entry->encryption_options, options)) {
241 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
242 << entry->encryption_options;
Paul Crowley77df7f22020-01-23 15:29:30 -0800243 return false;
Paul Crowleya50f6c32019-10-24 23:21:44 -0700244 }
Eric Biggerseb566d02020-07-06 13:46:38 -0700245 if ((options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
246 !IsEmmcStorage(entry->blk_device)) {
247 LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove "
248 "this flag from the device's fstab";
249 return false;
250 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800251 return true;
Eric Biggers83a73d72019-09-30 13:06:47 -0700252}
253
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800254static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options,
255 const KeyBuffer& key, EncryptionPolicy* policy) {
256 KeyBuffer ephemeral_wrapped_key;
257 if (options.use_hw_wrapped_key) {
258 if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) {
259 LOG(ERROR) << "Failed to get ephemeral wrapped key";
260 return false;
261 }
262 }
263 return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key,
264 policy);
265}
266
Eric Biggers83a73d72019-09-30 13:06:47 -0700267// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700268static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyeb241a12020-02-18 10:10:08 -0800269 // If we give the empty string, libfscrypt will use the default (currently XTS)
270 auto contents_mode = android::base::GetProperty("ro.crypto.volume.contents_mode", "");
271 // HEH as default was always a mistake. Use the libfscrypt default (CTS)
272 // for devices launching on versions above Android 10.
273 auto first_api_level = GetFirstApiLevel();
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700274 auto filenames_mode =
Paul Crowleyeb241a12020-02-18 10:10:08 -0800275 android::base::GetProperty("ro.crypto.volume.filenames_mode",
Eric Biggers72d07132020-08-10 10:55:56 -0700276 first_api_level > __ANDROID_API_Q__ ? "" : "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800277 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
Paul Crowleyeb241a12020-02-18 10:10:08 -0800278 contents_mode + ":" + filenames_mode);
279 if (!ParseOptionsForApiLevel(first_api_level, options_string, options)) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800280 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
281 return false;
282 }
Eric Biggerseb566d02020-07-06 13:46:38 -0700283 if (options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
284 LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove "
285 "this flag from ro.crypto.volume.options";
286 return false;
287 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800288 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700289}
290
Paul Crowleydf528a72016-03-09 09:31:37 -0800291static bool read_and_install_user_ce_key(userid_t user_id,
292 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800293 if (s_ce_policies.count(user_id) != 0) return true;
294 EncryptionOptions options;
295 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100296 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700297 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800298 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800299 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800300 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000301 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000302 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000303}
304
Paul Crowleydf528a72016-03-09 09:31:37 -0800305static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000306 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000307 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
308 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000309 return false;
310 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000311 return true;
312}
313
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600314static bool destroy_dir(const std::string& dir) {
315 LOG(DEBUG) << "Destroying: " << dir;
316 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
317 PLOG(ERROR) << "Failed to destroy " << dir;
318 return false;
319 }
320 return true;
321}
322
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000323// NB this assumes that there is only one thread listening for crypt commands, because
324// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000325static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800326 EncryptionOptions options;
327 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100328 KeyBuffer de_key, ce_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800329 if (!generateStorageKey(makeGen(options), &de_key)) return false;
330 if (!generateStorageKey(makeGen(options), &ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000331 if (create_ephemeral) {
332 // If the key should be created as ephemeral, don't store it.
333 s_ephemeral_users.insert(user_id);
334 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700335 auto const directory_path = get_ce_key_directory_path(user_id);
336 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
337 auto const paths = get_ce_key_paths(directory_path);
338 std::string ce_key_path;
339 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700340 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
341 ce_key))
342 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700343 fixate_user_ce_key(directory_path, ce_key_path, paths);
344 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700345 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700346 kEmptyAuthentication, de_key))
347 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100348 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800349 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800350 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800351 s_de_policies[user_id] = de_policy;
352 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800353 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800354 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000355 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000356 return true;
357}
358
Paul Crowley77df7f22020-01-23 15:29:30 -0800359static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
360 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000361 auto refi = key_map.find(user_id);
362 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700363 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000364 return false;
365 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800366 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000367 return true;
368}
369
Paul Crowleydf528a72016-03-09 09:31:37 -0800370static bool is_numeric(const char* name) {
371 for (const char* p = name; *p != '\0'; p++) {
372 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000373 }
374 return true;
375}
376
377static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800378 EncryptionOptions options;
379 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000380 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800381 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000382 if (!dirp) {
383 PLOG(ERROR) << "Unable to read de key directory";
384 return false;
385 }
386 for (;;) {
387 errno = 0;
388 auto entry = readdir(dirp.get());
389 if (!entry) {
390 if (errno) {
391 PLOG(ERROR) << "Unable to read de key directory";
392 return false;
393 }
394 break;
395 }
Eric Biggers6b840392020-11-02 15:11:06 -0800396 if (IsDotOrDotDot(*entry)) continue;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000397 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
398 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
399 continue;
400 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600401 userid_t user_id = std::stoi(entry->d_name);
Nikita Ioffef0550af2020-02-27 18:21:55 +0000402 auto key_path = de_dir + "/" + entry->d_name;
403 KeyBuffer de_key;
Eric Biggersf74373b2020-11-05 19:58:26 -0800404 if (!retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
Nikita Ioffef0550af2020-02-27 18:21:55 +0000405 EncryptionPolicy de_policy;
406 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
407 auto ret = s_de_policies.insert({user_id, de_policy});
408 if (!ret.second && ret.first->second != de_policy) {
409 LOG(ERROR) << "DE policy for user" << user_id << " changed";
410 return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000411 }
Nikita Ioffef0550af2020-02-27 18:21:55 +0000412 LOG(DEBUG) << "Installed de key for user " << user_id;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000413 }
Eric Biggersa701c452018-10-23 13:06:55 -0700414 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000415 // correct policy set on them, and that no rogue ones exist.
416 return true;
417}
418
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000419// Attempt to reinstall CE keys for users that we think are unlocked.
420static bool try_reload_ce_keys() {
421 for (const auto& it : s_ce_policies) {
422 if (!android::vold::reloadKeyFromSessionKeyring(DATA_MNT_POINT, it.second)) {
423 LOG(ERROR) << "Failed to load CE key from session keyring for user " << it.first;
424 return false;
425 }
426 }
427 return true;
428}
429
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700430bool fscrypt_initialize_systemwide_keys() {
431 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800432
Paul Crowley77df7f22020-01-23 15:29:30 -0800433 EncryptionOptions options;
434 if (!get_data_file_encryption_options(&options)) return false;
435
436 KeyBuffer device_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800437 if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
Eric Biggersf74373b2020-11-05 19:58:26 -0800438 makeGen(options), &device_key))
Paul Crowley77df7f22020-01-23 15:29:30 -0800439 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800440
Paul Crowley5e53ff62019-10-24 14:55:17 -0700441 EncryptionPolicy device_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800442 if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700443
Paul Crowley5e53ff62019-10-24 14:55:17 -0700444 std::string options_string;
445 if (!OptionsToString(device_policy.options, &options_string)) {
446 LOG(ERROR) << "Unable to serialize options";
447 return false;
448 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800449 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700450 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700451
Paul Crowley77df7f22020-01-23 15:29:30 -0800452 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700453 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700454 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800455
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700456 KeyBuffer per_boot_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800457 if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800458 EncryptionPolicy per_boot_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800459 if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700460 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800461 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
462 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700463 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
464
Paul Crowley76107cb2016-02-09 10:04:39 +0000465 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800466}
467
Eric Biggersa701c452018-10-23 13:06:55 -0700468bool fscrypt_init_user0() {
469 LOG(DEBUG) << "fscrypt_init_user0";
470 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000471 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
472 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
473 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700474 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000475 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000476 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700477 // TODO: switch to loading only DE_0 here once framework makes
478 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000479 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000480 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700481 // We can only safely prepare DE storage here, since CE keys are probably
482 // entangled with user credentials. The framework will always prepare CE
483 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700484 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700485 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000486 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700487 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700488
489 // If this is a non-FBE device that recently left an emulated mode,
490 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700491 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
492 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700493 }
494
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000495 // In some scenarios (e.g. userspace reboot) we might unmount userdata
496 // without doing a hard reboot. If CE keys were stored in fs keyring then
497 // they will be lost after unmount. Attempt to re-install them.
498 if (fscrypt_is_native() && android::vold::isFsKeyringSupported()) {
499 if (!try_reload_ce_keys()) return false;
500 }
501
Paul Crowley76107cb2016-02-09 10:04:39 +0000502 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000503}
504
Eric Biggersa701c452018-10-23 13:06:55 -0700505bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
506 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
507 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000508 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000509 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000510 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800511 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700512 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800513 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000514 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000515 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800516 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000517 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000518 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000519 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000520 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800521}
522
Eric Biggersce368682019-04-03 15:44:06 -0700523// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700524// in the case where the keys are being put in the session keyring (rather in
525// the newer filesystem-level keyrings), because removing a key from the session
526// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
527// was already set up. So to remove the per-file keys and make the files
528// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700529//
530// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
531// objects systemwide. This is overkill, but it's the best available method
532// currently. Don't use drop_caches mode "3" because that also evicts pagecache
533// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700534static void drop_caches_if_needed() {
535 if (android::vold::isFsKeyringSupported()) {
536 return;
537 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100538 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700539 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100540 PLOG(ERROR) << "Failed to drop caches during key eviction";
541 }
542}
543
Andrew Scull7ec25c72016-10-31 10:28:25 +0000544static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000545 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800546 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000547 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800548 if (lookup_policy(s_ce_policies, user_id, &policy)) {
549 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700550 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000551 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800552 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000553 return success;
554}
555
Eric Biggersa701c452018-10-23 13:06:55 -0700556bool fscrypt_destroy_user_key(userid_t user_id) {
557 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
558 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000559 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000560 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000561 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000562 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800563 EncryptionPolicy de_policy;
564 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
565 android::vold::evictKey(DATA_MNT_POINT, de_policy);
566 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000567 auto it = s_ephemeral_users.find(user_id);
568 if (it != s_ephemeral_users.end()) {
569 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000570 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700571 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700572 success &= android::vold::destroyKey(path);
573 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700574 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700575 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700576 success &= android::vold::destroyKey(de_key_path);
577 } else {
578 LOG(INFO) << "Not present so not erasing: " << de_key_path;
579 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100580 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000581 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100582}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800583
Paul Crowley76107cb2016-02-09 10:04:39 +0000584static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700585 if (chmod(path.c_str(), 0000) != 0) {
586 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000587 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700588 }
589#if EMULATED_USES_SELINUX
590 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
591 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000592 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700593 }
594#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000595 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700596}
597
Paul Crowley76107cb2016-02-09 10:04:39 +0000598static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700599 if (chmod(path.c_str(), mode) != 0) {
600 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000601 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700602 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700603 }
604#if EMULATED_USES_SELINUX
605 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
606 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000607 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700608 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700609 }
610#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000611 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700612}
613
Paul Crowley3b71fc52017-10-09 10:55:21 -0700614static bool parse_hex(const std::string& hex, std::string* result) {
615 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800616 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000617 return true;
618 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800619 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800620 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000621 return false;
622 }
623 return true;
624}
625
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700626static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
627 const std::string& token_hex, const std::string& secret_hex) {
628 std::string token, secret;
629 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
630 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
631 if (secret.empty()) {
632 return kEmptyAuthentication;
633 } else {
634 return android::vold::KeyAuthentication(token, secret);
635 }
636}
637
Paul Crowley3aa914d2017-10-09 16:35:51 -0700638static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
639 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
640}
641
Paul Crowley26a53882017-10-26 11:16:39 -0700642static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
643 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
644}
645
Paul Crowley3aa914d2017-10-09 16:35:51 -0700646static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700647 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700648 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
649 std::string secdiscardable_hash;
650 if (android::vold::pathExists(secdiscardable_path)) {
651 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
652 return false;
653 } else {
Eric Biggersfec0c0e2021-02-16 15:59:17 -0800654 if (!android::vold::MkdirsSync(secdiscardable_path, 0700)) return false;
Paul Crowley26a53882017-10-26 11:16:39 -0700655 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
656 return false;
657 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700658 auto key_path = volkey_path(misc_path, volume_uuid);
Eric Biggersfec0c0e2021-02-16 15:59:17 -0800659 if (!android::vold::MkdirsSync(key_path, 0700)) return false;
Paul Crowley26a53882017-10-26 11:16:39 -0700660 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700661
Paul Crowley77df7f22020-01-23 15:29:30 -0800662 EncryptionOptions options;
663 if (!get_volume_file_encryption_options(&options)) return false;
664 KeyBuffer key;
Eric Biggersf74373b2020-11-05 19:58:26 -0800665 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800666 return false;
667 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800668 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700669}
670
671static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700672 auto path = volkey_path(misc_path, volume_uuid);
673 if (!android::vold::pathExists(path)) return true;
674 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700675}
676
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700677static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
678 const android::vold::KeyAuthentication& retrieve_auth,
679 const android::vold::KeyAuthentication& store_auth) {
680 if (s_ephemeral_users.count(user_id) != 0) return true;
681 auto const directory_path = get_ce_key_directory_path(user_id);
682 KeyBuffer ce_key;
683 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Eric Biggersf74373b2020-11-05 19:58:26 -0800684 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700685 LOG(DEBUG) << "Successfully retrieved key";
686 // TODO(147732812): Remove this once Locksettingservice is fixed.
687 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
688 // changed from swipe to none or vice-versa
Eric Biggersf74373b2020-11-05 19:58:26 -0800689 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700690 LOG(DEBUG) << "Successfully retrieved key with empty auth";
691 } else {
692 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
693 return false;
694 }
695 auto const paths = get_ce_key_paths(directory_path);
696 std::string ce_key_path;
697 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
698 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
699 return false;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700700 return true;
701}
702
Eric Biggersa701c452018-10-23 13:06:55 -0700703bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700704 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700705 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700706 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700707 if (!fscrypt_is_native()) return true;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700708 auto auth = authentication_from_hex(token_hex, secret_hex);
709 if (!auth) return false;
710 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
711}
712
713bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
714 const std::string& secret_hex) {
715 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
716 << " token_present=" << (token_hex != "!");
717 if (!fscrypt_is_native()) return true;
718 auto auth = authentication_from_hex(token_hex, secret_hex);
719 if (!auth) return false;
720 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Paul Crowleya3630362016-05-17 14:17:56 -0700721}
722
Eric Biggersa701c452018-10-23 13:06:55 -0700723bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
724 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
725 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700726 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700727 auto const directory_path = get_ce_key_directory_path(user_id);
728 auto const paths = get_ce_key_paths(directory_path);
729 if (paths.empty()) {
730 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
731 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000732 }
Paul Crowleya3630362016-05-17 14:17:56 -0700733 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000734 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000735}
736
Jeff Sharkey47695b22016-02-01 17:02:29 -0700737// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700738bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700739 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700740 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700741 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700742 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800743 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000744 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000745 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000746 }
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700747 auto auth = authentication_from_hex(token_hex, secret_hex);
748 if (!auth) return false;
749 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000750 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000751 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800752 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700753 } else {
754 // When in emulation mode, we just use chmod. However, we also
755 // unlock directories when not in emulation mode, to bring devices
756 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000757 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800758 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700759 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
760 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700761 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000762 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700763 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800764 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000765 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800766}
767
Jeff Sharkey47695b22016-02-01 17:02:29 -0700768// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700769bool fscrypt_lock_user_key(userid_t user_id) {
770 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
771 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000772 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700773 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800774 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000775 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800776 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700777 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
778 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000779 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000780 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800781 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800782 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700783
Paul Crowley76107cb2016-02-09 10:04:39 +0000784 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800785}
786
Paul Crowley82b41ff2017-10-20 08:17:54 -0700787static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
788 userid_t user_id, int flags) {
789 if (0 != android::vold::ForkExecvp(
790 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
791 std::to_string(user_id), std::to_string(flags)})) {
792 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700793 return false;
794 }
795 return true;
796}
797
Eric Biggersa701c452018-10-23 13:06:55 -0700798bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700799 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700800 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800801 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700802
Paul Crowley82b41ff2017-10-20 08:17:54 -0700803 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600804 // DE_sys key
805 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
806 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
807 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600808
809 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700810 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
811 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800812 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700813 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
814
Paul Crowley3b71fc52017-10-09 10:55:21 -0700815 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700816 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600817#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700818 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700819 multiuser_get_uid(user_id, AID_EVERYBODY)))
820 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600821#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700822 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600823
Paul Crowley6b756ce2017-10-05 14:07:09 -0700824 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
825 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800826 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700827 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000828 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000829
Eric Biggersa701c452018-10-23 13:06:55 -0700830 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700831 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700832 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800833 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700834 if (!EnsurePolicy(de_policy, system_de_path)) return false;
835 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
836 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700837 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700838 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700839 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700840 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700841 }
Paul Crowley285956f2016-01-20 13:12:38 +0000842 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800843
Paul Crowley82b41ff2017-10-20 08:17:54 -0700844 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600845 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700846 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
847 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800848 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600849 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
850 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800851
Paul Crowley3b71fc52017-10-09 10:55:21 -0700852 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700853 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
854 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800855 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700856 }
Dhiraj Jadhav72005fd2021-02-18 04:57:03 +0000857 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
Martijn Coenenfd9cdbf2020-02-11 14:20:29 +0100858
Paul Crowley76107cb2016-02-09 10:04:39 +0000859 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700860
Eric Biggersa701c452018-10-23 13:06:55 -0700861 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700862 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700863 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800864 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700865 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
866 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
867 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700868 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700869 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700870 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700871 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
872 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700873 }
Paul Crowley1a965262017-10-13 13:49:50 -0700874
875 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700876 // Now that credentials have been installed, we can run restorecon
877 // over these paths
878 // NOTE: these paths need to be kept in sync with libselinux
879 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700880 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700881 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700882 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800883 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700884 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800885
Paul Crowley76107cb2016-02-09 10:04:39 +0000886 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800887}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600888
Eric Biggersa701c452018-10-23 13:06:55 -0700889bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
890 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600891 << ", user " << user_id << ", flags " << flags;
892 bool res = true;
893
Paul Crowley82b41ff2017-10-20 08:17:54 -0700894 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
895
896 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700897 // CE_n key
898 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
899 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800900 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700901 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
902 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
903
904 res &= destroy_dir(media_ce_path);
905 res &= destroy_dir(user_ce_path);
906 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700907 res &= destroy_dir(system_ce_path);
908 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800909 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700910 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700911 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700912 res &= destroy_volkey(misc_ce_path, volume_uuid);
913 }
Paul Crowley8e550662017-10-16 17:01:44 -0700914 }
915 }
916
Paul Crowley82b41ff2017-10-20 08:17:54 -0700917 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600918 // DE_sys key
919 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
920 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
921 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600922
923 // DE_n key
924 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
925 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800926 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600927 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
928
Paul Crowley8e550662017-10-16 17:01:44 -0700929 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700930 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600931 res &= destroy_dir(system_legacy_path);
932#if MANAGE_MISC_DIRS
933 res &= destroy_dir(misc_legacy_path);
934#endif
935 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600936 res &= destroy_dir(system_de_path);
937 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800938 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700939 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700940 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700941 res &= destroy_volkey(misc_de_path, volume_uuid);
942 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600943 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600944 }
945
946 return res;
947}
Rubin Xu2436e272017-04-27 20:43:10 +0100948
Paul Crowleyc6433a22017-10-24 14:54:43 -0700949static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
950 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
951 if (!dirp) {
952 PLOG(ERROR) << "Unable to open directory: " + directory_path;
953 return false;
954 }
955 bool res = true;
956 for (;;) {
957 errno = 0;
958 auto const entry = readdir(dirp.get());
959 if (!entry) {
960 if (errno) {
961 PLOG(ERROR) << "Unable to read directory: " + directory_path;
962 return false;
963 }
964 break;
965 }
Eric Biggers6b840392020-11-02 15:11:06 -0800966 if (IsDotOrDotDot(*entry)) continue;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700967 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
968 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
969 continue;
970 }
971 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
972 }
973 return res;
974}
975
Eric Biggersa701c452018-10-23 13:06:55 -0700976bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700977 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700978 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700979 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
980 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700981 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
982 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
983 return res;
984}