blob: ff8c1f470af504519f72f5180198fdb5fe9f2d22 [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
Tommy Chiua98464f2019-03-26 14:14:19 +0800465 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowley76107cb2016-02-09 10:04:39 +0000466 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800467}
468
Eric Biggersa701c452018-10-23 13:06:55 -0700469bool fscrypt_init_user0() {
470 LOG(DEBUG) << "fscrypt_init_user0";
471 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000472 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
473 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
474 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700475 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000476 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000477 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700478 // TODO: switch to loading only DE_0 here once framework makes
479 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000480 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000481 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700482 // We can only safely prepare DE storage here, since CE keys are probably
483 // entangled with user credentials. The framework will always prepare CE
484 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700485 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700486 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000487 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700488 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700489
490 // If this is a non-FBE device that recently left an emulated mode,
491 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700492 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
493 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700494 }
495
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000496 // In some scenarios (e.g. userspace reboot) we might unmount userdata
497 // without doing a hard reboot. If CE keys were stored in fs keyring then
498 // they will be lost after unmount. Attempt to re-install them.
499 if (fscrypt_is_native() && android::vold::isFsKeyringSupported()) {
500 if (!try_reload_ce_keys()) return false;
501 }
502
Paul Crowley76107cb2016-02-09 10:04:39 +0000503 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000504}
505
Eric Biggersa701c452018-10-23 13:06:55 -0700506bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
507 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
508 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000509 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000510 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000511 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800512 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700513 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800514 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000515 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000516 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800517 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000518 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000519 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000520 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000521 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800522}
523
Eric Biggersce368682019-04-03 15:44:06 -0700524// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700525// in the case where the keys are being put in the session keyring (rather in
526// the newer filesystem-level keyrings), because removing a key from the session
527// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
528// was already set up. So to remove the per-file keys and make the files
529// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700530//
531// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
532// objects systemwide. This is overkill, but it's the best available method
533// currently. Don't use drop_caches mode "3" because that also evicts pagecache
534// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700535static void drop_caches_if_needed() {
536 if (android::vold::isFsKeyringSupported()) {
537 return;
538 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100539 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700540 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100541 PLOG(ERROR) << "Failed to drop caches during key eviction";
542 }
543}
544
Andrew Scull7ec25c72016-10-31 10:28:25 +0000545static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000546 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800547 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000548 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800549 if (lookup_policy(s_ce_policies, user_id, &policy)) {
550 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700551 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000552 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800553 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000554 return success;
555}
556
Eric Biggersa701c452018-10-23 13:06:55 -0700557bool fscrypt_destroy_user_key(userid_t user_id) {
558 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
559 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000560 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000561 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000562 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000563 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800564 EncryptionPolicy de_policy;
565 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
566 android::vold::evictKey(DATA_MNT_POINT, de_policy);
567 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000568 auto it = s_ephemeral_users.find(user_id);
569 if (it != s_ephemeral_users.end()) {
570 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000571 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700572 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700573 success &= android::vold::destroyKey(path);
574 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700575 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700576 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700577 success &= android::vold::destroyKey(de_key_path);
578 } else {
579 LOG(INFO) << "Not present so not erasing: " << de_key_path;
580 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100581 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000582 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100583}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800584
Paul Crowley76107cb2016-02-09 10:04:39 +0000585static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700586 if (chmod(path.c_str(), 0000) != 0) {
587 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000588 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700589 }
590#if EMULATED_USES_SELINUX
591 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
592 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000593 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700594 }
595#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000596 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700597}
598
Paul Crowley76107cb2016-02-09 10:04:39 +0000599static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700600 if (chmod(path.c_str(), mode) != 0) {
601 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000602 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700603 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700604 }
605#if EMULATED_USES_SELINUX
606 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
607 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000608 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700609 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700610 }
611#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000612 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700613}
614
Paul Crowley3b71fc52017-10-09 10:55:21 -0700615static bool parse_hex(const std::string& hex, std::string* result) {
616 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800617 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000618 return true;
619 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800620 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800621 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000622 return false;
623 }
624 return true;
625}
626
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700627static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
628 const std::string& token_hex, const std::string& secret_hex) {
629 std::string token, secret;
630 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
631 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
632 if (secret.empty()) {
633 return kEmptyAuthentication;
634 } else {
635 return android::vold::KeyAuthentication(token, secret);
636 }
637}
638
Paul Crowley3aa914d2017-10-09 16:35:51 -0700639static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
640 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
641}
642
Paul Crowley26a53882017-10-26 11:16:39 -0700643static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
644 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
645}
646
Paul Crowley3aa914d2017-10-09 16:35:51 -0700647static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700648 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700649 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
650 std::string secdiscardable_hash;
651 if (android::vold::pathExists(secdiscardable_path)) {
652 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
653 return false;
654 } else {
655 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
656 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
657 return false;
658 }
659 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
660 return false;
661 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700662 auto key_path = volkey_path(misc_path, volume_uuid);
663 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
664 PLOG(ERROR) << "Creating directories for: " << key_path;
665 return false;
666 }
Paul Crowley26a53882017-10-26 11:16:39 -0700667 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700668
Paul Crowley77df7f22020-01-23 15:29:30 -0800669 EncryptionOptions options;
670 if (!get_volume_file_encryption_options(&options)) return false;
671 KeyBuffer key;
Eric Biggersf74373b2020-11-05 19:58:26 -0800672 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800673 return false;
674 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800675 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700676}
677
678static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700679 auto path = volkey_path(misc_path, volume_uuid);
680 if (!android::vold::pathExists(path)) return true;
681 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700682}
683
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700684static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
685 const android::vold::KeyAuthentication& retrieve_auth,
686 const android::vold::KeyAuthentication& store_auth) {
687 if (s_ephemeral_users.count(user_id) != 0) return true;
688 auto const directory_path = get_ce_key_directory_path(user_id);
689 KeyBuffer ce_key;
690 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Eric Biggersf74373b2020-11-05 19:58:26 -0800691 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700692 LOG(DEBUG) << "Successfully retrieved key";
693 // TODO(147732812): Remove this once Locksettingservice is fixed.
694 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
695 // changed from swipe to none or vice-versa
Eric Biggersf74373b2020-11-05 19:58:26 -0800696 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700697 LOG(DEBUG) << "Successfully retrieved key with empty auth";
698 } else {
699 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
700 return false;
701 }
702 auto const paths = get_ce_key_paths(directory_path);
703 std::string ce_key_path;
704 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
705 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
706 return false;
707 if (!android::vold::FsyncDirectory(directory_path)) return false;
708 return true;
709}
710
Eric Biggersa701c452018-10-23 13:06:55 -0700711bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700712 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700713 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700714 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700715 if (!fscrypt_is_native()) return true;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700716 auto auth = authentication_from_hex(token_hex, secret_hex);
717 if (!auth) return false;
718 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
719}
720
721bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
722 const std::string& secret_hex) {
723 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
724 << " token_present=" << (token_hex != "!");
725 if (!fscrypt_is_native()) return true;
726 auto auth = authentication_from_hex(token_hex, secret_hex);
727 if (!auth) return false;
728 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Paul Crowleya3630362016-05-17 14:17:56 -0700729}
730
Eric Biggersa701c452018-10-23 13:06:55 -0700731bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
732 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
733 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700734 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700735 auto const directory_path = get_ce_key_directory_path(user_id);
736 auto const paths = get_ce_key_paths(directory_path);
737 if (paths.empty()) {
738 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
739 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000740 }
Paul Crowleya3630362016-05-17 14:17:56 -0700741 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000742 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000743}
744
Jeff Sharkey47695b22016-02-01 17:02:29 -0700745// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700746bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700747 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700748 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700749 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700750 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800751 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000752 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000753 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000754 }
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700755 auto auth = authentication_from_hex(token_hex, secret_hex);
756 if (!auth) return false;
757 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000758 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000759 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800760 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700761 } else {
762 // When in emulation mode, we just use chmod. However, we also
763 // unlock directories when not in emulation mode, to bring devices
764 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000765 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800766 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700767 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
768 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700769 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000770 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700771 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800772 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000773 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800774}
775
Jeff Sharkey47695b22016-02-01 17:02:29 -0700776// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700777bool fscrypt_lock_user_key(userid_t user_id) {
778 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
779 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000780 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700781 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800782 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000783 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800784 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700785 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
786 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000787 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000788 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800789 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800790 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700791
Paul Crowley76107cb2016-02-09 10:04:39 +0000792 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800793}
794
Paul Crowley82b41ff2017-10-20 08:17:54 -0700795static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
796 userid_t user_id, int flags) {
Alan Stokesbe3db7b2020-02-07 09:29:38 +0000797 // TODO(b/141677108): Remove this & make it the default behavior
798 if (android::base::GetProperty("ro.vold.level_from_user", "0") == "1") {
799 flags |= android::os::IVold::STORAGE_FLAG_LEVEL_FROM_USER;
800 }
801
Paul Crowley82b41ff2017-10-20 08:17:54 -0700802 if (0 != android::vold::ForkExecvp(
803 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
804 std::to_string(user_id), std::to_string(flags)})) {
805 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700806 return false;
807 }
808 return true;
809}
810
Eric Biggersa701c452018-10-23 13:06:55 -0700811bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700812 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700813 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800814 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700815
Paul Crowley82b41ff2017-10-20 08:17:54 -0700816 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600817 // DE_sys key
818 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
819 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
820 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600821
822 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700823 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
824 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800825 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700826 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
827
Paul Crowley3b71fc52017-10-09 10:55:21 -0700828 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700829 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600830#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700831 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700832 multiuser_get_uid(user_id, AID_EVERYBODY)))
833 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600834#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700835 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600836
Paul Crowley6b756ce2017-10-05 14:07:09 -0700837 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
838 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800839 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700840 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000841 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000842
Eric Biggersa701c452018-10-23 13:06:55 -0700843 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700844 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700845 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800846 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700847 if (!EnsurePolicy(de_policy, system_de_path)) return false;
848 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
849 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700850 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700851 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700852 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700853 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700854 }
Paul Crowley285956f2016-01-20 13:12:38 +0000855 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800856
Paul Crowley82b41ff2017-10-20 08:17:54 -0700857 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600858 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700859 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
860 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800861 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600862 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
863 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800864
Paul Crowley3b71fc52017-10-09 10:55:21 -0700865 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700866 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
867 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800868 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700869 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000870 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
Martijn Coenenfd9cdbf2020-02-11 14:20:29 +0100871
Paul Crowley76107cb2016-02-09 10:04:39 +0000872 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700873
Eric Biggersa701c452018-10-23 13:06:55 -0700874 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700875 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700876 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800877 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700878 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 Crowley3aa914d2017-10-09 16:35:51 -0700881 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700882 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700883 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700884 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
885 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700886 }
Paul Crowley1a965262017-10-13 13:49:50 -0700887
888 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700889 // Now that credentials have been installed, we can run restorecon
890 // over these paths
891 // NOTE: these paths need to be kept in sync with libselinux
892 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700893 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700894 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700895 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800896 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700897 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800898
Paul Crowley76107cb2016-02-09 10:04:39 +0000899 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800900}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600901
Eric Biggersa701c452018-10-23 13:06:55 -0700902bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
903 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600904 << ", user " << user_id << ", flags " << flags;
905 bool res = true;
906
Paul Crowley82b41ff2017-10-20 08:17:54 -0700907 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
908
909 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700910 // CE_n key
911 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
912 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800913 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700914 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
915 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
916
917 res &= destroy_dir(media_ce_path);
918 res &= destroy_dir(user_ce_path);
919 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700920 res &= destroy_dir(system_ce_path);
921 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800922 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700923 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700924 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700925 res &= destroy_volkey(misc_ce_path, volume_uuid);
926 }
Paul Crowley8e550662017-10-16 17:01:44 -0700927 }
928 }
929
Paul Crowley82b41ff2017-10-20 08:17:54 -0700930 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600931 // DE_sys key
932 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
933 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
934 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600935
936 // DE_n key
937 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
938 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800939 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600940 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
941
Paul Crowley8e550662017-10-16 17:01:44 -0700942 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700943 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600944 res &= destroy_dir(system_legacy_path);
945#if MANAGE_MISC_DIRS
946 res &= destroy_dir(misc_legacy_path);
947#endif
948 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600949 res &= destroy_dir(system_de_path);
950 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800951 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700952 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700953 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700954 res &= destroy_volkey(misc_de_path, volume_uuid);
955 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600956 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600957 }
958
959 return res;
960}
Rubin Xu2436e272017-04-27 20:43:10 +0100961
Paul Crowleyc6433a22017-10-24 14:54:43 -0700962static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
963 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
964 if (!dirp) {
965 PLOG(ERROR) << "Unable to open directory: " + directory_path;
966 return false;
967 }
968 bool res = true;
969 for (;;) {
970 errno = 0;
971 auto const entry = readdir(dirp.get());
972 if (!entry) {
973 if (errno) {
974 PLOG(ERROR) << "Unable to read directory: " + directory_path;
975 return false;
976 }
977 break;
978 }
Eric Biggers6b840392020-11-02 15:11:06 -0800979 if (IsDotOrDotDot(*entry)) continue;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700980 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}