blob: 9794b0e0fe7330d45e8300d4460aeebde64c527a [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Shivaprasad Hongal92292622018-07-05 14:49:12 -070019#include "Keymaster.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000020#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080022#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070023#include "VoldUtil.h"
24
Paul Crowleya3630362016-05-17 14:17:56 -070025#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000026#include <map>
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -070027#include <optional>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000028#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070029#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080030#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000032
Paul Crowleydf528a72016-03-09 09:31:37 -080033#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070036#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070037#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080038#include <sys/mount.h>
39#include <sys/stat.h>
40#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000042
Paul Crowley480fcd22015-08-24 14:53:28 +010043#include <private/android_filesystem_config.h>
Martijn Coenenaee40512020-02-18 16:29:25 +010044#include <private/android_projectid_config.h>
Paul Crowley480fcd22015-08-24 14:53:28 +010045
Paul Crowley82b41ff2017-10-20 08:17:54 -070046#include "android/os/IVold.h"
47
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070048#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060049#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070050
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080051#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070052#include <cutils/properties.h>
53
Eric Biggersa701c452018-10-23 13:06:55 -070054#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070055#include <keyutils.h>
Eric Biggers006eed82020-07-06 13:46:38 -070056#include <libdm/dm.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080057
Elliott Hughes7e128fb2015-12-04 15:50:53 -080058#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080059#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070060#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080061#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070062#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080063#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000064
Eric Biggers006eed82020-07-06 13:46:38 -070065using android::base::Basename;
66using android::base::Realpath;
67using android::base::StartsWith;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080068using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080069using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley77df7f22020-01-23 15:29:30 -080070using android::vold::BuildDataPath;
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 Crowleyb3d018a2020-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 Biggers006eed82020-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 }
144 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
145 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
146 continue;
147 }
148 result.emplace_back(directory_path + "/" + entry->d_name);
149 }
150 std::sort(result.begin(), result.end());
151 std::reverse(result.begin(), result.end());
152 return result;
153}
154
155static std::string get_ce_key_current_path(const std::string& directory_path) {
156 return directory_path + "/current";
157}
158
159static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700160 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700161 if (paths.empty()) {
162 *ce_key_path = get_ce_key_current_path(directory_path);
163 return true;
164 }
165 for (unsigned int i = 0; i < UINT_MAX; i++) {
166 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
167 if (paths[0] < candidate) {
168 *ce_key_path = candidate;
169 return true;
170 }
171 }
172 return false;
173}
174
175// Discard all keys but the named one; rename it to canonical name.
176// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700177static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700178 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700179 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700180 if (other_path != to_fix) {
181 android::vold::destroyKey(other_path);
182 }
183 }
184 auto const current_path = get_ce_key_current_path(directory_path);
185 if (to_fix != current_path) {
186 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
187 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
188 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800189 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700190 }
191 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800192 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700193}
194
195static bool read_and_fixate_user_ce_key(userid_t user_id,
196 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700197 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700198 auto const directory_path = get_ce_key_directory_path(user_id);
199 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700200 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700201 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700202 if (retrieveKey(ce_key_path, auth, ce_key, false)) {
Paul Crowleya3630362016-05-17 14:17:56 -0700203 LOG(DEBUG) << "Successfully retrieved key";
204 fixate_user_ce_key(directory_path, ce_key_path, paths);
205 return true;
206 }
207 }
208 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
209 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100210}
211
Eric Biggers006eed82020-07-06 13:46:38 -0700212static bool IsEmmcStorage(const std::string& blk_device) {
213 // Handle symlinks.
214 std::string real_path;
215 if (!Realpath(blk_device, &real_path)) {
216 real_path = blk_device;
217 }
218
219 // Handle logical volumes.
220 auto& dm = DeviceMapper::Instance();
221 for (;;) {
222 auto parent = dm.GetParentBlockDeviceByPath(real_path);
223 if (!parent.has_value()) break;
224 real_path = *parent;
225 }
226
227 // Now we should have the "real" block device.
228 LOG(DEBUG) << "IsEmmcStorage(): blk_device = " << blk_device << ", real_path=" << real_path;
229 return StartsWith(Basename(real_path), "mmcblk");
230}
231
Eric Biggers83a73d72019-09-30 13:06:47 -0700232// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley77df7f22020-01-23 15:29:30 -0800233static bool get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700234 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
235 if (entry == nullptr) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800236 LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
237 return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700238 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700239 if (!ParseOptions(entry->encryption_options, options)) {
240 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
241 << entry->encryption_options;
Paul Crowley77df7f22020-01-23 15:29:30 -0800242 return false;
Paul Crowleya50f6c32019-10-24 23:21:44 -0700243 }
Eric Biggers006eed82020-07-06 13:46:38 -0700244 if ((options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
245 !IsEmmcStorage(entry->blk_device)) {
246 LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove "
247 "this flag from the device's fstab";
248 return false;
249 }
Steven Laver7fe4cdb2020-02-14 13:18:26 -0800250 if (options->version == 1) {
251 options->use_hw_wrapped_key =
252 GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
253 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800254 return true;
Eric Biggers83a73d72019-09-30 13:06:47 -0700255}
256
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800257static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options,
258 const KeyBuffer& key, EncryptionPolicy* policy) {
259 KeyBuffer ephemeral_wrapped_key;
260 if (options.use_hw_wrapped_key) {
261 if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) {
262 LOG(ERROR) << "Failed to get ephemeral wrapped key";
263 return false;
264 }
265 }
266 return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key,
267 policy);
268}
269
Eric Biggers83a73d72019-09-30 13:06:47 -0700270// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700271static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyeb241a12020-02-18 10:10:08 -0800272 // If we give the empty string, libfscrypt will use the default (currently XTS)
273 auto contents_mode = android::base::GetProperty("ro.crypto.volume.contents_mode", "");
274 // HEH as default was always a mistake. Use the libfscrypt default (CTS)
275 // for devices launching on versions above Android 10.
276 auto first_api_level = GetFirstApiLevel();
277 constexpr uint64_t pre_gki_level = 29;
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700278 auto filenames_mode =
Paul Crowleyeb241a12020-02-18 10:10:08 -0800279 android::base::GetProperty("ro.crypto.volume.filenames_mode",
280 first_api_level > pre_gki_level ? "" : "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800281 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
Paul Crowleyeb241a12020-02-18 10:10:08 -0800282 contents_mode + ":" + filenames_mode);
283 if (!ParseOptionsForApiLevel(first_api_level, options_string, options)) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800284 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
285 return false;
286 }
Eric Biggers006eed82020-07-06 13:46:38 -0700287 if (options->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
288 LOG(ERROR) << "The emmc_optimized encryption flag is only allowed on eMMC storage. Remove "
289 "this flag from ro.crypto.volume.options";
290 return false;
291 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800292 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700293}
294
Neeraj Sonic480f912018-12-14 15:18:15 +0530295bool is_metadata_wrapped_key_supported() {
296 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
297}
298
Paul Crowleydf528a72016-03-09 09:31:37 -0800299static bool read_and_install_user_ce_key(userid_t user_id,
300 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800301 if (s_ce_policies.count(user_id) != 0) return true;
302 EncryptionOptions options;
303 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100304 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700305 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800306 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800307 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800308 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000309 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000310 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000311}
312
Paul Crowleydf528a72016-03-09 09:31:37 -0800313static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000314 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000315 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
316 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000317 return false;
318 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000319 return true;
320}
321
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600322static bool destroy_dir(const std::string& dir) {
323 LOG(DEBUG) << "Destroying: " << dir;
324 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
325 PLOG(ERROR) << "Failed to destroy " << dir;
326 return false;
327 }
328 return true;
329}
330
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000331// NB this assumes that there is only one thread listening for crypt commands, because
332// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000333static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800334 EncryptionOptions options;
335 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100336 KeyBuffer de_key, ce_key;
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800337 if (!generateStorageKey(makeGen(options), &de_key)) return false;
338 if (!generateStorageKey(makeGen(options), &ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000339 if (create_ephemeral) {
340 // If the key should be created as ephemeral, don't store it.
341 s_ephemeral_users.insert(user_id);
342 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700343 auto const directory_path = get_ce_key_directory_path(user_id);
344 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
345 auto const paths = get_ce_key_paths(directory_path);
346 std::string ce_key_path;
347 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700348 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
349 ce_key))
350 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700351 fixate_user_ce_key(directory_path, ce_key_path, paths);
352 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700353 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700354 kEmptyAuthentication, de_key))
355 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100356 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800357 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800358 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800359 s_de_policies[user_id] = de_policy;
360 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800361 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800362 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000363 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000364 return true;
365}
366
Paul Crowley77df7f22020-01-23 15:29:30 -0800367static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
368 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000369 auto refi = key_map.find(user_id);
370 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700371 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000372 return false;
373 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800374 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000375 return true;
376}
377
Paul Crowleydf528a72016-03-09 09:31:37 -0800378static bool is_numeric(const char* name) {
379 for (const char* p = name; *p != '\0'; p++) {
380 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000381 }
382 return true;
383}
384
385static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800386 EncryptionOptions options;
387 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000388 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800389 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000390 if (!dirp) {
391 PLOG(ERROR) << "Unable to read de key directory";
392 return false;
393 }
394 for (;;) {
395 errno = 0;
396 auto entry = readdir(dirp.get());
397 if (!entry) {
398 if (errno) {
399 PLOG(ERROR) << "Unable to read de key directory";
400 return false;
401 }
402 break;
403 }
404 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
405 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
406 continue;
407 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600408 userid_t user_id = std::stoi(entry->d_name);
Nikita Ioffe1eaea5a2020-02-27 18:21:55 +0000409 auto key_path = de_dir + "/" + entry->d_name;
410 KeyBuffer de_key;
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700411 if (!retrieveKey(key_path, kEmptyAuthentication, &de_key, false)) return false;
Nikita Ioffe1eaea5a2020-02-27 18:21:55 +0000412 EncryptionPolicy de_policy;
413 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
414 auto ret = s_de_policies.insert({user_id, de_policy});
415 if (!ret.second && ret.first->second != de_policy) {
416 LOG(ERROR) << "DE policy for user" << user_id << " changed";
417 return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000418 }
Nikita Ioffe1eaea5a2020-02-27 18:21:55 +0000419 LOG(DEBUG) << "Installed de key for user " << user_id;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000420 }
Eric Biggersa701c452018-10-23 13:06:55 -0700421 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000422 // correct policy set on them, and that no rogue ones exist.
423 return true;
424}
425
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000426// Attempt to reinstall CE keys for users that we think are unlocked.
427static bool try_reload_ce_keys() {
428 for (const auto& it : s_ce_policies) {
429 if (!android::vold::reloadKeyFromSessionKeyring(DATA_MNT_POINT, it.second)) {
430 LOG(ERROR) << "Failed to load CE key from session keyring for user " << it.first;
431 return false;
432 }
433 }
434 return true;
435}
436
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700437bool fscrypt_initialize_systemwide_keys() {
438 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800439
Paul Crowley77df7f22020-01-23 15:29:30 -0800440 EncryptionOptions options;
441 if (!get_data_file_encryption_options(&options)) return false;
442
443 KeyBuffer device_key;
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800444 if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700445 makeGen(options), &device_key, false))
Paul Crowley77df7f22020-01-23 15:29:30 -0800446 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800447
Paul Crowley5e53ff62019-10-24 14:55:17 -0700448 EncryptionPolicy device_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800449 if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800450
Paul Crowley5e53ff62019-10-24 14:55:17 -0700451 std::string options_string;
452 if (!OptionsToString(device_policy.options, &options_string)) {
453 LOG(ERROR) << "Unable to serialize options";
454 return false;
455 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800456 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700457 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700458
Paul Crowley77df7f22020-01-23 15:29:30 -0800459 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700460 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700461 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800462
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700463 KeyBuffer per_boot_key;
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800464 if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800465 EncryptionPolicy per_boot_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800466 if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700467 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800468 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
469 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700470 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
471
Tommy Chiua98464f2019-03-26 14:14:19 +0800472 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowley76107cb2016-02-09 10:04:39 +0000473 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800474}
475
Eric Biggersa701c452018-10-23 13:06:55 -0700476bool fscrypt_init_user0() {
477 LOG(DEBUG) << "fscrypt_init_user0";
478 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000479 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
480 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
481 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700482 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000483 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000484 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700485 // TODO: switch to loading only DE_0 here once framework makes
486 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000487 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000488 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700489 // We can only safely prepare DE storage here, since CE keys are probably
490 // entangled with user credentials. The framework will always prepare CE
491 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700492 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700493 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000494 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700495 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700496
497 // If this is a non-FBE device that recently left an emulated mode,
498 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700499 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
500 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700501 }
502
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000503 // In some scenarios (e.g. userspace reboot) we might unmount userdata
504 // without doing a hard reboot. If CE keys were stored in fs keyring then
505 // they will be lost after unmount. Attempt to re-install them.
506 if (fscrypt_is_native() && android::vold::isFsKeyringSupported()) {
507 if (!try_reload_ce_keys()) return false;
508 }
509
Paul Crowley76107cb2016-02-09 10:04:39 +0000510 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000511}
512
Eric Biggersa701c452018-10-23 13:06:55 -0700513bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
514 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
515 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000516 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000517 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000518 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800519 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700520 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800521 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000522 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000523 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800524 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000525 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000526 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000527 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000528 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800529}
530
Eric Biggersce368682019-04-03 15:44:06 -0700531// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700532// in the case where the keys are being put in the session keyring (rather in
533// the newer filesystem-level keyrings), because removing a key from the session
534// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
535// was already set up. So to remove the per-file keys and make the files
536// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700537//
538// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
539// objects systemwide. This is overkill, but it's the best available method
540// currently. Don't use drop_caches mode "3" because that also evicts pagecache
541// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700542static void drop_caches_if_needed() {
543 if (android::vold::isFsKeyringSupported()) {
544 return;
545 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100546 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700547 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100548 PLOG(ERROR) << "Failed to drop caches during key eviction";
549 }
550}
551
Andrew Scull7ec25c72016-10-31 10:28:25 +0000552static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000553 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800554 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000555 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800556 if (lookup_policy(s_ce_policies, user_id, &policy)) {
557 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700558 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000559 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800560 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000561 return success;
562}
563
Eric Biggersa701c452018-10-23 13:06:55 -0700564bool fscrypt_destroy_user_key(userid_t user_id) {
565 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
566 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000567 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000568 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000569 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000570 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800571 EncryptionPolicy de_policy;
572 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
573 android::vold::evictKey(DATA_MNT_POINT, de_policy);
574 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000575 auto it = s_ephemeral_users.find(user_id);
576 if (it != s_ephemeral_users.end()) {
577 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000578 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700579 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700580 success &= android::vold::destroyKey(path);
581 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700582 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700583 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700584 success &= android::vold::destroyKey(de_key_path);
585 } else {
586 LOG(INFO) << "Not present so not erasing: " << de_key_path;
587 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100588 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000589 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100590}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800591
Paul Crowley76107cb2016-02-09 10:04:39 +0000592static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700593 if (chmod(path.c_str(), 0000) != 0) {
594 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000595 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700596 }
597#if EMULATED_USES_SELINUX
598 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
599 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000600 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700601 }
602#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000603 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700604}
605
Paul Crowley76107cb2016-02-09 10:04:39 +0000606static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700607 if (chmod(path.c_str(), mode) != 0) {
608 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000609 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700610 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700611 }
612#if EMULATED_USES_SELINUX
613 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
614 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000615 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700616 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700617 }
618#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000619 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700620}
621
Paul Crowley3b71fc52017-10-09 10:55:21 -0700622static bool parse_hex(const std::string& hex, std::string* result) {
623 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800624 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000625 return true;
626 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800627 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800628 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000629 return false;
630 }
631 return true;
632}
633
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700634static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
635 const std::string& token_hex, const std::string& secret_hex) {
636 std::string token, secret;
637 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
638 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
639 if (secret.empty()) {
640 return kEmptyAuthentication;
641 } else {
642 return android::vold::KeyAuthentication(token, secret);
643 }
644}
645
Paul Crowley3aa914d2017-10-09 16:35:51 -0700646static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
647 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
648}
649
Paul Crowley26a53882017-10-26 11:16:39 -0700650static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
651 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
652}
653
Paul Crowley3aa914d2017-10-09 16:35:51 -0700654static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800655 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700656 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
657 std::string secdiscardable_hash;
658 if (android::vold::pathExists(secdiscardable_path)) {
659 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
660 return false;
661 } else {
662 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
663 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
664 return false;
665 }
666 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
667 return false;
668 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700669 auto key_path = volkey_path(misc_path, volume_uuid);
670 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
671 PLOG(ERROR) << "Creating directories for: " << key_path;
672 return false;
673 }
Paul Crowley26a53882017-10-26 11:16:39 -0700674 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700675
Paul Crowley77df7f22020-01-23 15:29:30 -0800676 EncryptionOptions options;
677 if (!get_volume_file_encryption_options(&options)) return false;
678 KeyBuffer key;
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700679 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key, false))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800680 return false;
681 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800682 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700683}
684
685static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700686 auto path = volkey_path(misc_path, volume_uuid);
687 if (!android::vold::pathExists(path)) return true;
688 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700689}
690
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700691static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
692 const android::vold::KeyAuthentication& retrieve_auth,
693 const android::vold::KeyAuthentication& store_auth) {
694 if (s_ephemeral_users.count(user_id) != 0) return true;
695 auto const directory_path = get_ce_key_directory_path(user_id);
696 KeyBuffer ce_key;
697 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700698 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key, false)) {
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700699 LOG(DEBUG) << "Successfully retrieved key";
700 // TODO(147732812): Remove this once Locksettingservice is fixed.
701 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
702 // changed from swipe to none or vice-versa
Eric Biggers2c9d6d62020-10-29 12:59:28 -0700703 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key, false)) {
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700704 LOG(DEBUG) << "Successfully retrieved key with empty auth";
705 } else {
706 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
707 return false;
708 }
709 auto const paths = get_ce_key_paths(directory_path);
Steven Laver4771d982020-01-23 17:03:42 -0800710
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700711 std::string ce_key_path;
712 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
713 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
714 return false;
715 if (!android::vold::FsyncDirectory(directory_path)) return false;
716 return true;
717}
718
Eric Biggersa701c452018-10-23 13:06:55 -0700719bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700720 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700721 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700722 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700723 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700724 auto auth = authentication_from_hex(token_hex, secret_hex);
725 if (!auth) return false;
726 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
Paul Crowleya3630362016-05-17 14:17:56 -0700727}
728
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800729bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700730 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800731 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700732 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800733 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700734 auto auth = authentication_from_hex(token_hex, secret_hex);
735 if (!auth) return false;
736 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700737}
738
Eric Biggersa701c452018-10-23 13:06:55 -0700739bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
740 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
741 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700742 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700743 auto const directory_path = get_ce_key_directory_path(user_id);
744 auto const paths = get_ce_key_paths(directory_path);
745 if (paths.empty()) {
746 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
747 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000748 }
Paul Crowleya3630362016-05-17 14:17:56 -0700749 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000750 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000751}
752
Jeff Sharkey47695b22016-02-01 17:02:29 -0700753// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700754bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700755 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700756 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700757 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700758 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800759 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000760 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000761 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000762 }
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700763 auto auth = authentication_from_hex(token_hex, secret_hex);
764 if (!auth) return false;
765 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000766 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000767 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800768 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700769 } else {
770 // When in emulation mode, we just use chmod. However, we also
771 // unlock directories when not in emulation mode, to bring devices
772 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000773 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800774 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700775 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
776 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700777 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000778 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700779 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800780 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000781 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800782}
783
Jeff Sharkey47695b22016-02-01 17:02:29 -0700784// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700785bool fscrypt_lock_user_key(userid_t user_id) {
786 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
787 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000788 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700789 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800790 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000791 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800792 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700793 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
794 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000795 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000796 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800797 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800798 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700799
Paul Crowley76107cb2016-02-09 10:04:39 +0000800 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800801}
802
Paul Crowley82b41ff2017-10-20 08:17:54 -0700803static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
804 userid_t user_id, int flags) {
805 if (0 != android::vold::ForkExecvp(
806 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
807 std::to_string(user_id), std::to_string(flags)})) {
808 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700809 return false;
810 }
811 return true;
812}
813
Eric Biggersa701c452018-10-23 13:06:55 -0700814bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700815 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700816 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800817 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700818
Paul Crowley82b41ff2017-10-20 08:17:54 -0700819 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600820 // DE_sys key
821 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
822 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
823 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600824
825 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700826 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
827 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800828 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700829 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
830
Paul Crowley3b71fc52017-10-09 10:55:21 -0700831 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700832 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600833#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700834 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700835 multiuser_get_uid(user_id, AID_EVERYBODY)))
836 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600837#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700838 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600839
Paul Crowley6b756ce2017-10-05 14:07:09 -0700840 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
841 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800842 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700843 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000844 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000845
Eric Biggersa701c452018-10-23 13:06:55 -0700846 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700847 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700848 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800849 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700850 if (!EnsurePolicy(de_policy, system_de_path)) return false;
851 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
852 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700853 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800854 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700855 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700856 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700857 }
Paul Crowley285956f2016-01-20 13:12:38 +0000858 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800859
Paul Crowley82b41ff2017-10-20 08:17:54 -0700860 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600861 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700862 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
863 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800864 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600865 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
866 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800867
Paul Crowley3b71fc52017-10-09 10:55:21 -0700868 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700869 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
870 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800871 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700872 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000873 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
Martijn Coenenfd9cdbf2020-02-11 14:20:29 +0100874
Paul Crowley76107cb2016-02-09 10:04:39 +0000875 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700876
Eric Biggersa701c452018-10-23 13:06:55 -0700877 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700878 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700879 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800880 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700881 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
882 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
883 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700884 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800885 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700886 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700887 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
888 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700889 }
Paul Crowley1a965262017-10-13 13:49:50 -0700890
891 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700892 // Now that credentials have been installed, we can run restorecon
893 // over these paths
894 // NOTE: these paths need to be kept in sync with libselinux
895 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700896 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700897 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700898 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800899 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700900 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800901
Paul Crowley76107cb2016-02-09 10:04:39 +0000902 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800903}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600904
Eric Biggersa701c452018-10-23 13:06:55 -0700905bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
906 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600907 << ", user " << user_id << ", flags " << flags;
908 bool res = true;
909
Paul Crowley82b41ff2017-10-20 08:17:54 -0700910 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
911
912 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700913 // CE_n key
914 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
915 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800916 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700917 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
918 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
919
920 res &= destroy_dir(media_ce_path);
921 res &= destroy_dir(user_ce_path);
922 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700923 res &= destroy_dir(system_ce_path);
924 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800925 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700926 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700927 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700928 res &= destroy_volkey(misc_ce_path, volume_uuid);
929 }
Paul Crowley8e550662017-10-16 17:01:44 -0700930 }
931 }
932
Paul Crowley82b41ff2017-10-20 08:17:54 -0700933 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600934 // DE_sys key
935 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
936 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
937 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600938
939 // DE_n key
940 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
941 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800942 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600943 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
944
Paul Crowley8e550662017-10-16 17:01:44 -0700945 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700946 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600947 res &= destroy_dir(system_legacy_path);
948#if MANAGE_MISC_DIRS
949 res &= destroy_dir(misc_legacy_path);
950#endif
951 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600952 res &= destroy_dir(system_de_path);
953 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800954 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700955 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700956 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700957 res &= destroy_volkey(misc_de_path, volume_uuid);
958 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600959 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600960 }
961
962 return res;
963}
Rubin Xu2436e272017-04-27 20:43:10 +0100964
Paul Crowleyc6433a22017-10-24 14:54:43 -0700965static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
966 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
967 if (!dirp) {
968 PLOG(ERROR) << "Unable to open directory: " + directory_path;
969 return false;
970 }
971 bool res = true;
972 for (;;) {
973 errno = 0;
974 auto const entry = readdir(dirp.get());
975 if (!entry) {
976 if (errno) {
977 PLOG(ERROR) << "Unable to read directory: " + directory_path;
978 return false;
979 }
980 break;
981 }
982 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
983 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
984 continue;
985 }
986 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
987 }
988 return res;
989}
990
Eric Biggersa701c452018-10-23 13:06:55 -0700991bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700992 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700993 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700994 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
995 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700996 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
997 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
998 return res;
999}