blob: 183878a7f2af320b1819b6f05352b38c9083135f [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>
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>
43
Paul Crowley82b41ff2017-10-20 08:17:54 -070044#include "android/os/IVold.h"
45
Paul Lawrence731a7a22015-04-28 22:14:15 +000046#include "cryptfs.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>
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
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080064using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080065using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000066using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010067using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070068using android::vold::Keymaster;
69using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080070using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070071using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080072
Paul Lawrence731a7a22015-04-28 22:14:15 +000073namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000074
Eric Biggersa701c452018-10-23 13:06:55 -070075const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080076const std::string device_key_path = device_key_dir + "/key";
77const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080078
Paul Crowleydf528a72016-03-09 09:31:37 -080079const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
80const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070081const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000082
Paul Crowley26a53882017-10-26 11:16:39 -070083const std::string systemwide_volume_key_dir =
84 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
85
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070086bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080087
Paul Crowleydf528a72016-03-09 09:31:37 -080088// Some users are ephemeral, don't try to wipe their keys from disk
89std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080090
Paul Crowleydf528a72016-03-09 09:31:37 -080091// Map user ids to key references
92std::map<userid_t, std::string> s_de_key_raw_refs;
93std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070094// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +010095std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +000096
Paul Crowley14c8c072018-09-18 13:30:21 -070097} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000098
Eric Biggersa701c452018-10-23 13:06:55 -070099static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000100 return property_get_bool("persist.sys.emulate_fbe", false);
101}
102
Paul Crowley3b71fc52017-10-09 10:55:21 -0700103static const char* escape_empty(const std::string& value) {
104 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000105}
106
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000107static std::string get_de_key_path(userid_t user_id) {
108 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
109}
110
Paul Crowleya3630362016-05-17 14:17:56 -0700111static std::string get_ce_key_directory_path(userid_t user_id) {
112 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
113}
114
115// Returns the keys newest first
116static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
117 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
118 if (!dirp) {
119 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
120 return std::vector<std::string>();
121 }
122 std::vector<std::string> result;
123 for (;;) {
124 errno = 0;
125 auto const entry = readdir(dirp.get());
126 if (!entry) {
127 if (errno) {
128 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
129 return std::vector<std::string>();
130 }
131 break;
132 }
133 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
134 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
135 continue;
136 }
137 result.emplace_back(directory_path + "/" + entry->d_name);
138 }
139 std::sort(result.begin(), result.end());
140 std::reverse(result.begin(), result.end());
141 return result;
142}
143
144static std::string get_ce_key_current_path(const std::string& directory_path) {
145 return directory_path + "/current";
146}
147
148static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700149 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700150 if (paths.empty()) {
151 *ce_key_path = get_ce_key_current_path(directory_path);
152 return true;
153 }
154 for (unsigned int i = 0; i < UINT_MAX; i++) {
155 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
156 if (paths[0] < candidate) {
157 *ce_key_path = candidate;
158 return true;
159 }
160 }
161 return false;
162}
163
164// Discard all keys but the named one; rename it to canonical name.
165// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700166static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700167 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700169 if (other_path != to_fix) {
170 android::vold::destroyKey(other_path);
171 }
172 }
173 auto const current_path = get_ce_key_current_path(directory_path);
174 if (to_fix != current_path) {
175 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
176 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
177 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800178 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700179 }
180 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800181 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700182}
183
184static bool read_and_fixate_user_ce_key(userid_t user_id,
185 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700187 auto const directory_path = get_ce_key_directory_path(user_id);
188 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
191 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
192 LOG(DEBUG) << "Successfully retrieved key";
193 fixate_user_ce_key(directory_path, ce_key_path, paths);
194 return true;
195 }
196 }
197 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
198 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100199}
200
Eric Biggers83a73d72019-09-30 13:06:47 -0700201// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700202static void get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700203 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
204 if (entry == nullptr) {
205 return;
206 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700207 if (!ParseOptions(entry->encryption_options, options)) {
208 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
209 << entry->encryption_options;
210 return;
211 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700212}
213
214// Retrieve the version to use for encryption policies on the /data filesystem.
215static int get_data_file_policy_version(void) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700216 EncryptionOptions options;
217 get_data_file_encryption_options(&options);
218 return options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700219}
220
221// Retrieve the options to use for encryption policies on adoptable storage.
Steven Laverd4eb3412019-11-09 19:05:21 -0800222static bool get_volume_file_encryption_options(EncryptionOptions* options, int flags) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700223 auto contents_mode =
Barani Muthukumaranff525592020-01-29 14:38:31 -0800224 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700225 auto filenames_mode =
Eric Biggers83a73d72019-09-30 13:06:47 -0700226 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700227 return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
228 contents_mode + ":" + filenames_mode + ":v1"),
229 options);
Eric Biggers83a73d72019-09-30 13:06:47 -0700230}
231
Eric Biggersf3dc4202019-09-30 13:05:58 -0700232// Install a key for use by encrypted files on the /data filesystem.
233static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700234 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700235}
236
237// Evict a key for use by encrypted files on the /data filesystem.
238static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700239 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700240}
241
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700242bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800243 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700244}
245
Barani Muthukumaranff525592020-01-29 14:38:31 -0800246static bool is_wrapped_key_supported_external() {
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700247 return false;
248}
249
Neeraj Sonic480f912018-12-14 15:18:15 +0530250bool is_metadata_wrapped_key_supported() {
251 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
252}
253
Paul Crowleydf528a72016-03-09 09:31:37 -0800254static bool read_and_install_user_ce_key(userid_t user_id,
255 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000256 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100257 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700258 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000259 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700260
261 if (is_wrapped_key_supported()) {
262 KeyBuffer ephemeral_wrapped_key;
263 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
264 LOG(ERROR) << "Failed to export ce key";
265 return false;
266 }
267 ce_key = std::move(ephemeral_wrapped_key);
268 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700269 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100270 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000271 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000272 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000273 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000274}
275
Paul Crowleydf528a72016-03-09 09:31:37 -0800276static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000277 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000278 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
279 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000280 return false;
281 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000282 return true;
283}
284
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600285static bool destroy_dir(const std::string& dir) {
286 LOG(DEBUG) << "Destroying: " << dir;
287 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
288 PLOG(ERROR) << "Failed to destroy " << dir;
289 return false;
290 }
291 return true;
292}
293
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000294// NB this assumes that there is only one thread listening for crypt commands, because
295// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000296static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100297 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700298
299 if(is_wrapped_key_supported()) {
300 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
301 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
302 } else {
303 if (!android::vold::randomKey(&de_key)) return false;
304 if (!android::vold::randomKey(&ce_key)) return false;
305 }
306
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000307 if (create_ephemeral) {
308 // If the key should be created as ephemeral, don't store it.
309 s_ephemeral_users.insert(user_id);
310 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700311 auto const directory_path = get_ce_key_directory_path(user_id);
312 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
313 auto const paths = get_ce_key_paths(directory_path);
314 std::string ce_key_path;
315 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700316 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
317 ce_key))
318 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700319 fixate_user_ce_key(directory_path, ce_key_path, paths);
320 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700321 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700322 kEmptyAuthentication, de_key))
323 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100324 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700325
326 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000327 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000328 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700329
330 if (is_wrapped_key_supported()) {
331 KeyBuffer ephemeral_wrapped_de_key;
332 KeyBuffer ephemeral_wrapped_ce_key;
333
334 /* Export and install the DE keys */
335 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
336 LOG(ERROR) << "Failed to export de_key";
337 return false;
338 }
339 /* Export and install the CE keys */
340 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
341 LOG(ERROR) << "Failed to export de_key";
342 return false;
343 }
344
345 de_key = std::move(ephemeral_wrapped_de_key);
346 ce_key = std::move(ephemeral_wrapped_ce_key);
347 }
Steven Lavered747be2019-10-24 16:40:01 -0700348 if (!install_data_key(de_key, &de_raw_ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700349 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700350 s_ce_keys[user_id] = std::move(ce_key);
351
352 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000353 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700354
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 Crowleydf528a72016-03-09 09:31:37 -0800359static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
360 std::string* raw_ref) {
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 Crowleya051eb72016-03-08 16:08:32 -0800366 *raw_ref = 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() {
378 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800379 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000380 if (!dirp) {
381 PLOG(ERROR) << "Unable to read de key directory";
382 return false;
383 }
384 for (;;) {
385 errno = 0;
386 auto entry = readdir(dirp.get());
387 if (!entry) {
388 if (errno) {
389 PLOG(ERROR) << "Unable to read de key directory";
390 return false;
391 }
392 break;
393 }
394 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
395 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
396 continue;
397 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600398 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000399 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000400 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100401 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800402 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000403 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700404 if (is_wrapped_key_supported()) {
405 KeyBuffer ephemeral_wrapped_key;
406 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
407 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
408 return false;
409 }
410 key = std::move(ephemeral_wrapped_key);
411 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700412 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000413 s_de_key_raw_refs[user_id] = raw_ref;
414 LOG(DEBUG) << "Installed de key for user " << user_id;
415 }
416 }
Eric Biggersa701c452018-10-23 13:06:55 -0700417 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000418 // correct policy set on them, and that no rogue ones exist.
419 return true;
420}
421
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700422bool fscrypt_initialize_systemwide_keys() {
423 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700424 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800425
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700426 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000427 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000428 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800429 }
430
Paul Crowley5e53ff62019-10-24 14:55:17 -0700431 EncryptionPolicy device_policy;
432 get_data_file_encryption_options(&device_policy.options);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700433 wrapped_key_supported = is_wrapped_key_supported();
434
Eric Biggers83a73d72019-09-30 13:06:47 -0700435 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700436 device_key_temp, "", device_policy.options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800437 &device_policy.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700438 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800439
Paul Crowley5e53ff62019-10-24 14:55:17 -0700440 std::string options_string;
441 if (!OptionsToString(device_policy.options, &options_string)) {
442 LOG(ERROR) << "Unable to serialize options";
443 return false;
444 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700445 std::string options_filename = std::string("/data") + fscrypt_key_mode;
446 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700447
Eric Biggersa701c452018-10-23 13:06:55 -0700448 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700449 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700450 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800451
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700452 KeyBuffer per_boot_key;
453 if (!android::vold::randomKey(&per_boot_key)) return false;
454 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700455 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700456 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
457 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
458 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
459
Tommy Chiua98464f2019-03-26 14:14:19 +0800460 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700461 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000462 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800463}
464
Eric Biggersa701c452018-10-23 13:06:55 -0700465bool fscrypt_init_user0() {
466 LOG(DEBUG) << "fscrypt_init_user0";
467 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000468 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
469 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
470 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700471 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000472 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000473 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700474 // TODO: switch to loading only DE_0 here once framework makes
475 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000476 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000477 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700478 // We can only safely prepare DE storage here, since CE keys are probably
479 // entangled with user credentials. The framework will always prepare CE
480 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700481 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700482 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000483 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700484 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700485
486 // If this is a non-FBE device that recently left an emulated mode,
487 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700488 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
489 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700490 }
491
Paul Crowley76107cb2016-02-09 10:04:39 +0000492 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000493}
494
Eric Biggersa701c452018-10-23 13:06:55 -0700495bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
496 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
497 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000498 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000499 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000500 // FIXME test for existence of key that is not loaded yet
501 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700502 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800503 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000504 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000505 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800506 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000507 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000508 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000509 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000510 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800511}
512
Eric Biggersce368682019-04-03 15:44:06 -0700513// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700514// in the case where the keys are being put in the session keyring (rather in
515// the newer filesystem-level keyrings), because removing a key from the session
516// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
517// was already set up. So to remove the per-file keys and make the files
518// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700519//
520// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
521// objects systemwide. This is overkill, but it's the best available method
522// currently. Don't use drop_caches mode "3" because that also evicts pagecache
523// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700524static void drop_caches_if_needed() {
525 if (android::vold::isFsKeyringSupported()) {
526 return;
527 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100528 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700529 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100530 PLOG(ERROR) << "Failed to drop caches during key eviction";
531 }
532}
533
Andrew Scull7ec25c72016-10-31 10:28:25 +0000534static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000535 bool success = true;
536 std::string raw_ref;
537 // If we haven't loaded the CE key, no need to evict it.
538 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700539 success &= evict_data_key(raw_ref);
540 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000541 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700542
543 if(is_wrapped_key_supported()) {
544 KeyBuffer key;
545 key = s_ce_keys[user_id];
546
547 std::string keystr(key.data(), key.size());
548 Keymaster keymaster;
549
550 if (!keymaster) {
551 s_ce_keys.erase(user_id);
552 s_ce_key_raw_refs.erase(user_id);
553 return false;
554 }
555 keymaster.deleteKey(keystr);
556 }
557
558 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000559 s_ce_key_raw_refs.erase(user_id);
560 return success;
561}
562
Eric Biggersa701c452018-10-23 13:06:55 -0700563bool fscrypt_destroy_user_key(userid_t user_id) {
564 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
565 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000566 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000567 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000568 bool success = true;
569 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000570 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700571 success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_data_key(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000572 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000573 auto it = s_ephemeral_users.find(user_id);
574 if (it != s_ephemeral_users.end()) {
575 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000576 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700577 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700578 success &= android::vold::destroyKey(path);
579 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700580 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700581 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700582 success &= android::vold::destroyKey(de_key_path);
583 } else {
584 LOG(INFO) << "Not present so not erasing: " << de_key_path;
585 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100586 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000587 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100588}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800589
Paul Crowley76107cb2016-02-09 10:04:39 +0000590static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700591 if (chmod(path.c_str(), 0000) != 0) {
592 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000593 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700594 }
595#if EMULATED_USES_SELINUX
596 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
597 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000598 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700599 }
600#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000601 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700602}
603
Paul Crowley76107cb2016-02-09 10:04:39 +0000604static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700605 if (chmod(path.c_str(), mode) != 0) {
606 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000607 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700608 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700609 }
610#if EMULATED_USES_SELINUX
611 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
612 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000613 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700614 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700615 }
616#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000617 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700618}
619
Paul Crowley3b71fc52017-10-09 10:55:21 -0700620static bool parse_hex(const std::string& hex, std::string* result) {
621 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800622 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000623 return true;
624 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800625 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800626 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000627 return false;
628 }
629 return true;
630}
631
Paul Crowley3aa914d2017-10-09 16:35:51 -0700632static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
633 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
634}
635
Paul Crowley26a53882017-10-26 11:16:39 -0700636static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
637 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
638}
639
Paul Crowley3aa914d2017-10-09 16:35:51 -0700640static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800641 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700642 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
643 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700644 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700645 if (android::vold::pathExists(secdiscardable_path)) {
646 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
647 return false;
648 } else {
649 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
650 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
651 return false;
652 }
653 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
654 return false;
655 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700656 auto key_path = volkey_path(misc_path, volume_uuid);
657 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
658 PLOG(ERROR) << "Creating directories for: " << key_path;
659 return false;
660 }
Paul Crowley26a53882017-10-26 11:16:39 -0700661 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700662
Steven Laverd4eb3412019-11-09 19:05:21 -0800663 if (!get_volume_file_encryption_options(&policy->options, flags)) return false;
Barani Muthukumaranff525592020-01-29 14:38:31 -0800664 wrapped_key_supported = is_wrapped_key_supported_external();
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700665
Eric Biggers83a73d72019-09-30 13:06:47 -0700666 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley5e53ff62019-10-24 14:55:17 -0700667 volume_uuid, policy->options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800668 &policy->key_raw_ref, wrapped_key_supported);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700669}
670
671static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700672 auto path = volkey_path(misc_path, volume_uuid);
673 if (!android::vold::pathExists(path)) return true;
674 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700675}
676
Eric Biggersa701c452018-10-23 13:06:55 -0700677bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700678 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700679 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700680 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700681 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000682 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700683 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800684 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700685 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700686 auto auth =
687 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700688 auto const directory_path = get_ce_key_directory_path(user_id);
689 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700690
691 KeyBuffer ce_key;
692 if(is_wrapped_key_supported()) {
693 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
694 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
695 LOG(DEBUG) << "Successfully retrieved key";
696 } else {
697 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
698 LOG(DEBUG) << "Successfully retrieved key";
699 }
700 }
701 } else {
702 auto it = s_ce_keys.find(user_id);
703 if (it == s_ce_keys.end()) {
704 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
705 return false;
706 }
707 ce_key = it->second;
708 }
709
Paul Crowleya3630362016-05-17 14:17:56 -0700710 std::string ce_key_path;
711 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700712 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800713 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700714 return true;
715}
716
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800717bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700718 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800719 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700720 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800721 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700722 if (s_ephemeral_users.count(user_id) != 0) return true;
723 std::string token, secret;
724
725 if (!parse_hex(token_hex, &token)) return false;
726 if (!parse_hex(secret_hex, &secret)) return false;
727
728 if (is_wrapped_key_supported()) {
729 auto const directory_path = get_ce_key_directory_path(user_id);
730 auto const paths = get_ce_key_paths(directory_path);
731
732 KeyBuffer ce_key;
733 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
734
735 auto auth = android::vold::KeyAuthentication(token, secret);
736 /* Retrieve key while removing a pin. A secret is needed */
737 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
738 LOG(DEBUG) << "Successfully retrieved key";
739 } else {
740 /* Retrieve key when going None to swipe and vice versa when a
741 synthetic password is present */
742 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
743 LOG(DEBUG) << "Successfully retrieved key";
744 }
745 }
746
747 std::string ce_key_path;
748 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
749 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
750 return false;
751 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800752 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700753 }
754 return true;
755}
756
Eric Biggersa701c452018-10-23 13:06:55 -0700757bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
758 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
759 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700760 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700761 auto const directory_path = get_ce_key_directory_path(user_id);
762 auto const paths = get_ce_key_paths(directory_path);
763 if (paths.empty()) {
764 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
765 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000766 }
Paul Crowleya3630362016-05-17 14:17:56 -0700767 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000768 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000769}
770
Jeff Sharkey47695b22016-02-01 17:02:29 -0700771// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700772bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700773 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700774 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700775 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700776 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000777 if (s_ce_key_raw_refs.count(user_id) != 0) {
778 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000779 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000780 }
781 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800782 if (!parse_hex(token_hex, &token)) return false;
783 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000784 android::vold::KeyAuthentication auth(token, secret);
785 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000786 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000787 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800788 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700789 } else {
790 // When in emulation mode, we just use chmod. However, we also
791 // unlock directories when not in emulation mode, to bring devices
792 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000793 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800794 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700795 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
796 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700797 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000798 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700799 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800800 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000801 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800802}
803
Jeff Sharkey47695b22016-02-01 17:02:29 -0700804// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700805bool fscrypt_lock_user_key(userid_t user_id) {
806 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
807 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000808 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700809 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800810 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000811 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800812 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700813 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
814 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000815 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000816 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800817 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800818 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700819
Paul Crowley76107cb2016-02-09 10:04:39 +0000820 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800821}
822
Paul Crowley82b41ff2017-10-20 08:17:54 -0700823static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
824 userid_t user_id, int flags) {
825 if (0 != android::vold::ForkExecvp(
826 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
827 std::to_string(user_id), std::to_string(flags)})) {
828 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700829 return false;
830 }
831 return true;
832}
833
Eric Biggersa701c452018-10-23 13:06:55 -0700834bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700835 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700836 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800837 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700838
Paul Crowley82b41ff2017-10-20 08:17:54 -0700839 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600840 // DE_sys key
841 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
842 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
843 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600844
845 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700846 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
847 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800848 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700849 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
850
Paul Crowley3b71fc52017-10-09 10:55:21 -0700851 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700852 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600853#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700854 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700855 multiuser_get_uid(user_id, AID_EVERYBODY)))
856 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600857#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700858 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600859
Paul Crowley6b756ce2017-10-05 14:07:09 -0700860 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
861 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800862 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700863 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000864 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000865
Eric Biggersa701c452018-10-23 13:06:55 -0700866 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700867 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700868 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700869 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
870 return false;
871 get_data_file_encryption_options(&de_policy.options);
872 if (!EnsurePolicy(de_policy, system_de_path)) return false;
873 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
874 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700875 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800876 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700877 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700878 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700879 }
Paul Crowley285956f2016-01-20 13:12:38 +0000880 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800881
Paul Crowley82b41ff2017-10-20 08:17:54 -0700882 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600883 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700884 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
885 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800886 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600887 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
888 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800889
Paul Crowley3b71fc52017-10-09 10:55:21 -0700890 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700891 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
892 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800893 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700894 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000895 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
896 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700897
Eric Biggersa701c452018-10-23 13:06:55 -0700898 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700899 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700900 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700901 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
902 return false;
903 get_data_file_encryption_options(&ce_policy.options);
904 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
905 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
906 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700907
Paul Crowley3aa914d2017-10-09 16:35:51 -0700908 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800909 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700910 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700911 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
912 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700913 }
Paul Crowley1a965262017-10-13 13:49:50 -0700914
915 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700916 // Now that credentials have been installed, we can run restorecon
917 // over these paths
918 // NOTE: these paths need to be kept in sync with libselinux
919 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700920 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700921 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700922 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800923 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700924 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800925
Paul Crowley76107cb2016-02-09 10:04:39 +0000926 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800927}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600928
Eric Biggersa701c452018-10-23 13:06:55 -0700929bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
930 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600931 << ", user " << user_id << ", flags " << flags;
932 bool res = true;
933
Paul Crowley82b41ff2017-10-20 08:17:54 -0700934 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
935
936 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700937 // CE_n key
938 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
939 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800940 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700941 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
942 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
943
944 res &= destroy_dir(media_ce_path);
945 res &= destroy_dir(user_ce_path);
946 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700947 res &= destroy_dir(system_ce_path);
948 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800949 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700950 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700951 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700952 res &= destroy_volkey(misc_ce_path, volume_uuid);
953 }
Paul Crowley8e550662017-10-16 17:01:44 -0700954 }
955 }
956
Paul Crowley82b41ff2017-10-20 08:17:54 -0700957 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600958 // DE_sys key
959 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
960 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
961 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600962
963 // DE_n key
964 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
965 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800966 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600967 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
968
Paul Crowley8e550662017-10-16 17:01:44 -0700969 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700970 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600971 res &= destroy_dir(system_legacy_path);
972#if MANAGE_MISC_DIRS
973 res &= destroy_dir(misc_legacy_path);
974#endif
975 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600976 res &= destroy_dir(system_de_path);
977 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800978 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700979 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700980 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700981 res &= destroy_volkey(misc_de_path, volume_uuid);
982 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600983 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600984 }
985
986 return res;
987}
Rubin Xu2436e272017-04-27 20:43:10 +0100988
Paul Crowleyc6433a22017-10-24 14:54:43 -0700989static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
990 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
991 if (!dirp) {
992 PLOG(ERROR) << "Unable to open directory: " + directory_path;
993 return false;
994 }
995 bool res = true;
996 for (;;) {
997 errno = 0;
998 auto const entry = readdir(dirp.get());
999 if (!entry) {
1000 if (errno) {
1001 PLOG(ERROR) << "Unable to read directory: " + directory_path;
1002 return false;
1003 }
1004 break;
1005 }
1006 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
1007 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
1008 continue;
1009 }
1010 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
1011 }
1012 return res;
1013}
1014
Eric Biggersa701c452018-10-23 13:06:55 -07001015bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -07001016 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -07001017 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -07001018 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
1019 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -07001020 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
1021 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
1022 return res;
1023}