blob: 0d4f02e796aa0a57be7ed884f8709a76470247e6 [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Shivaprasad Hongal92292622018-07-05 14:49:12 -070019#include "Keymaster.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000020#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080022#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070023#include "VoldUtil.h"
Gaurav Kashyap38cbb942019-07-17 18:11:57 -070024#include "model/Disk.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070025
Paul Crowleya3630362016-05-17 14:17:56 -070026#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000027#include <map>
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>
44
Paul Crowley82b41ff2017-10-20 08:17:54 -070045#include "android/os/IVold.h"
46
Paul Lawrence731a7a22015-04-28 22:14:15 +000047#include "cryptfs.h"
48
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060050#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070051
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080052#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070053#include <cutils/properties.h>
54
Eric Biggersa701c452018-10-23 13:06:55 -070055#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070056#include <keyutils.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
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080065using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080066using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000067using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010068using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070069using android::vold::Keymaster;
70using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080071using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070072using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080073
Paul Lawrence731a7a22015-04-28 22:14:15 +000074namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000075
Eric Biggersa701c452018-10-23 13:06:55 -070076const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080077const std::string device_key_path = device_key_dir + "/key";
78const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080079
Paul Crowleydf528a72016-03-09 09:31:37 -080080const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
81const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070082const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000083
Paul Crowley26a53882017-10-26 11:16:39 -070084const std::string systemwide_volume_key_dir =
85 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
86
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070087bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080088
Paul Crowleydf528a72016-03-09 09:31:37 -080089// Some users are ephemeral, don't try to wipe their keys from disk
90std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080091
Paul Crowleydf528a72016-03-09 09:31:37 -080092// Map user ids to key references
93std::map<userid_t, std::string> s_de_key_raw_refs;
94std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070095// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +010096std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +000097
Paul Crowley14c8c072018-09-18 13:30:21 -070098} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000099
Eric Biggersa701c452018-10-23 13:06:55 -0700100static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000101 return property_get_bool("persist.sys.emulate_fbe", false);
102}
103
Paul Crowley3b71fc52017-10-09 10:55:21 -0700104static const char* escape_empty(const std::string& value) {
105 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000106}
107
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000108static std::string get_de_key_path(userid_t user_id) {
109 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
110}
111
Paul Crowleya3630362016-05-17 14:17:56 -0700112static std::string get_ce_key_directory_path(userid_t user_id) {
113 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
114}
115
116// Returns the keys newest first
117static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
118 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
119 if (!dirp) {
120 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
121 return std::vector<std::string>();
122 }
123 std::vector<std::string> result;
124 for (;;) {
125 errno = 0;
126 auto const entry = readdir(dirp.get());
127 if (!entry) {
128 if (errno) {
129 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
130 return std::vector<std::string>();
131 }
132 break;
133 }
134 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
135 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
136 continue;
137 }
138 result.emplace_back(directory_path + "/" + entry->d_name);
139 }
140 std::sort(result.begin(), result.end());
141 std::reverse(result.begin(), result.end());
142 return result;
143}
144
145static std::string get_ce_key_current_path(const std::string& directory_path) {
146 return directory_path + "/current";
147}
148
149static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700150 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700151 if (paths.empty()) {
152 *ce_key_path = get_ce_key_current_path(directory_path);
153 return true;
154 }
155 for (unsigned int i = 0; i < UINT_MAX; i++) {
156 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
157 if (paths[0] < candidate) {
158 *ce_key_path = candidate;
159 return true;
160 }
161 }
162 return false;
163}
164
165// Discard all keys but the named one; rename it to canonical name.
166// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700167static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700168 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700169 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700170 if (other_path != to_fix) {
171 android::vold::destroyKey(other_path);
172 }
173 }
174 auto const current_path = get_ce_key_current_path(directory_path);
175 if (to_fix != current_path) {
176 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
177 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
178 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800179 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700180 }
181 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800182 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700183}
184
185static bool read_and_fixate_user_ce_key(userid_t user_id,
186 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700187 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700188 auto const directory_path = get_ce_key_directory_path(user_id);
189 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700190 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700191 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
192 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
193 LOG(DEBUG) << "Successfully retrieved key";
194 fixate_user_ce_key(directory_path, ce_key_path, paths);
195 return true;
196 }
197 }
198 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
199 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100200}
201
Eric Biggers83a73d72019-09-30 13:06:47 -0700202// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700203static void get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700204 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
205 if (entry == nullptr) {
206 return;
207 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700208 if (!ParseOptions(entry->encryption_options, options)) {
209 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
210 << entry->encryption_options;
211 return;
212 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700213}
214
215// Retrieve the version to use for encryption policies on the /data filesystem.
216static int get_data_file_policy_version(void) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700217 EncryptionOptions options;
218 get_data_file_encryption_options(&options);
219 return options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700220}
221
222// Retrieve the options to use for encryption policies on adoptable storage.
Steven Laverd4eb3412019-11-09 19:05:21 -0800223static bool get_volume_file_encryption_options(EncryptionOptions* options, int flags) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700224 auto contents_mode =
Steven Laverd4eb3412019-11-09 19:05:21 -0800225 android::base::GetProperty("ro.crypto.volume.contents_mode",
226 is_ice_supported_external(flags) ? "ice" : "aes-256-xts");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700227 auto filenames_mode =
Eric Biggers83a73d72019-09-30 13:06:47 -0700228 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700229 return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
230 contents_mode + ":" + filenames_mode + ":v1"),
231 options);
Eric Biggers83a73d72019-09-30 13:06:47 -0700232}
233
Eric Biggersf3dc4202019-09-30 13:05:58 -0700234// Install a key for use by encrypted files on the /data filesystem.
235static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700236 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700237}
238
239// Evict a key for use by encrypted files on the /data filesystem.
240static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700241 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700242}
243
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700244bool is_ice_supported_external(int flags) {
245 /*
246 * Logic can be changed when more card controllers start supporting ICE.
247 * Until then, checking only for UFS card.
248 */
249 if ((flags & android::vold::Disk::Flags::kUfsCard) ==
250 android::vold::Disk::Flags::kUfsCard)
251 return true;
252 return false;
253}
254
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700255bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800256 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700257}
258
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700259bool is_wrapped_key_supported_external(int flags) {
260 if (is_ice_supported_external(flags))
261 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700262 return false;
263}
264
Neeraj Sonic480f912018-12-14 15:18:15 +0530265bool is_metadata_wrapped_key_supported() {
266 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
267}
268
Paul Crowleydf528a72016-03-09 09:31:37 -0800269static bool read_and_install_user_ce_key(userid_t user_id,
270 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000271 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100272 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700273 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000274 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700275
276 if (is_wrapped_key_supported()) {
277 KeyBuffer ephemeral_wrapped_key;
278 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
279 LOG(ERROR) << "Failed to export ce key";
280 return false;
281 }
282 ce_key = std::move(ephemeral_wrapped_key);
283 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700284 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100285 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000286 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000287 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000288 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000289}
290
Paul Crowleydf528a72016-03-09 09:31:37 -0800291static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000292 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000293 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
294 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000295 return false;
296 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000297 return true;
298}
299
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600300static bool destroy_dir(const std::string& dir) {
301 LOG(DEBUG) << "Destroying: " << dir;
302 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
303 PLOG(ERROR) << "Failed to destroy " << dir;
304 return false;
305 }
306 return true;
307}
308
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000309// NB this assumes that there is only one thread listening for crypt commands, because
310// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000311static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100312 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700313
314 if(is_wrapped_key_supported()) {
315 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
316 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
317 } else {
318 if (!android::vold::randomKey(&de_key)) return false;
319 if (!android::vold::randomKey(&ce_key)) return false;
320 }
321
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000322 if (create_ephemeral) {
323 // If the key should be created as ephemeral, don't store it.
324 s_ephemeral_users.insert(user_id);
325 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700326 auto const directory_path = get_ce_key_directory_path(user_id);
327 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
328 auto const paths = get_ce_key_paths(directory_path);
329 std::string ce_key_path;
330 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700331 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
332 ce_key))
333 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700334 fixate_user_ce_key(directory_path, ce_key_path, paths);
335 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700336 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700337 kEmptyAuthentication, de_key))
338 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100339 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700340
341 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000342 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000343 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700344
345 if (is_wrapped_key_supported()) {
346 KeyBuffer ephemeral_wrapped_de_key;
347 KeyBuffer ephemeral_wrapped_ce_key;
348
349 /* Export and install the DE keys */
350 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
351 LOG(ERROR) << "Failed to export de_key";
352 return false;
353 }
354 /* Export and install the CE keys */
355 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
356 LOG(ERROR) << "Failed to export de_key";
357 return false;
358 }
359
360 de_key = std::move(ephemeral_wrapped_de_key);
361 ce_key = std::move(ephemeral_wrapped_ce_key);
362 }
Steven Lavered747be2019-10-24 16:40:01 -0700363 if (!install_data_key(de_key, &de_raw_ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700364 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700365 s_ce_keys[user_id] = std::move(ce_key);
366
367 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000368 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700369
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000370 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000371 return true;
372}
373
Paul Crowleydf528a72016-03-09 09:31:37 -0800374static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
375 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000376 auto refi = key_map.find(user_id);
377 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700378 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000379 return false;
380 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800381 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000382 return true;
383}
384
Paul Crowleydf528a72016-03-09 09:31:37 -0800385static bool is_numeric(const char* name) {
386 for (const char* p = name; *p != '\0'; p++) {
387 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000388 }
389 return true;
390}
391
392static bool load_all_de_keys() {
393 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800394 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000395 if (!dirp) {
396 PLOG(ERROR) << "Unable to read de key directory";
397 return false;
398 }
399 for (;;) {
400 errno = 0;
401 auto entry = readdir(dirp.get());
402 if (!entry) {
403 if (errno) {
404 PLOG(ERROR) << "Unable to read de key directory";
405 return false;
406 }
407 break;
408 }
409 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
410 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
411 continue;
412 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600413 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000414 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000415 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100416 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800417 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000418 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700419 if (is_wrapped_key_supported()) {
420 KeyBuffer ephemeral_wrapped_key;
421 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
422 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
423 return false;
424 }
425 key = std::move(ephemeral_wrapped_key);
426 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700427 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000428 s_de_key_raw_refs[user_id] = raw_ref;
429 LOG(DEBUG) << "Installed de key for user " << user_id;
430 }
431 }
Eric Biggersa701c452018-10-23 13:06:55 -0700432 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000433 // correct policy set on them, and that no rogue ones exist.
434 return true;
435}
436
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700437bool fscrypt_initialize_systemwide_keys() {
438 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700439 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800440
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700441 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000442 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000443 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800444 }
445
Paul Crowley5e53ff62019-10-24 14:55:17 -0700446 EncryptionPolicy device_policy;
447 get_data_file_encryption_options(&device_policy.options);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700448 wrapped_key_supported = is_wrapped_key_supported();
449
Eric Biggers83a73d72019-09-30 13:06:47 -0700450 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700451 device_key_temp, "", device_policy.options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800452 &device_policy.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700453 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800454
Paul Crowley5e53ff62019-10-24 14:55:17 -0700455 std::string options_string;
456 if (!OptionsToString(device_policy.options, &options_string)) {
457 LOG(ERROR) << "Unable to serialize options";
458 return false;
459 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700460 std::string options_filename = std::string("/data") + fscrypt_key_mode;
461 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700462
Eric Biggersa701c452018-10-23 13:06:55 -0700463 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700464 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700465 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800466
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700467 KeyBuffer per_boot_key;
468 if (!android::vold::randomKey(&per_boot_key)) return false;
469 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700470 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700471 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
472 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
473 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
474
Tommy Chiua98464f2019-03-26 14:14:19 +0800475 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700476 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000477 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800478}
479
Eric Biggersa701c452018-10-23 13:06:55 -0700480bool fscrypt_init_user0() {
481 LOG(DEBUG) << "fscrypt_init_user0";
482 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000483 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
484 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
485 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700486 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000487 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000488 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700489 // TODO: switch to loading only DE_0 here once framework makes
490 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000491 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000492 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700493 // We can only safely prepare DE storage here, since CE keys are probably
494 // entangled with user credentials. The framework will always prepare CE
495 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700496 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700497 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000498 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700499 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700500
501 // If this is a non-FBE device that recently left an emulated mode,
502 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700503 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
504 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700505 }
506
Paul Crowley76107cb2016-02-09 10:04:39 +0000507 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000508}
509
Eric Biggersa701c452018-10-23 13:06:55 -0700510bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
511 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
512 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000513 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000514 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000515 // FIXME test for existence of key that is not loaded yet
516 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700517 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800518 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000519 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000520 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800521 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000522 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000523 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000524 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000525 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800526}
527
Eric Biggersce368682019-04-03 15:44:06 -0700528// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700529// in the case where the keys are being put in the session keyring (rather in
530// the newer filesystem-level keyrings), because removing a key from the session
531// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
532// was already set up. So to remove the per-file keys and make the files
533// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700534//
535// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
536// objects systemwide. This is overkill, but it's the best available method
537// currently. Don't use drop_caches mode "3" because that also evicts pagecache
538// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700539static void drop_caches_if_needed() {
540 if (android::vold::isFsKeyringSupported()) {
541 return;
542 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100543 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700544 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100545 PLOG(ERROR) << "Failed to drop caches during key eviction";
546 }
547}
548
Andrew Scull7ec25c72016-10-31 10:28:25 +0000549static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000550 bool success = true;
551 std::string raw_ref;
552 // If we haven't loaded the CE key, no need to evict it.
553 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700554 success &= evict_data_key(raw_ref);
555 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000556 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700557
558 if(is_wrapped_key_supported()) {
559 KeyBuffer key;
560 key = s_ce_keys[user_id];
561
562 std::string keystr(key.data(), key.size());
563 Keymaster keymaster;
564
565 if (!keymaster) {
566 s_ce_keys.erase(user_id);
567 s_ce_key_raw_refs.erase(user_id);
568 return false;
569 }
570 keymaster.deleteKey(keystr);
571 }
572
573 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000574 s_ce_key_raw_refs.erase(user_id);
575 return success;
576}
577
Eric Biggersa701c452018-10-23 13:06:55 -0700578bool fscrypt_destroy_user_key(userid_t user_id) {
579 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
580 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000581 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000582 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000583 bool success = true;
584 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000585 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700586 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 +0000587 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000588 auto it = s_ephemeral_users.find(user_id);
589 if (it != s_ephemeral_users.end()) {
590 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000591 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700592 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700593 success &= android::vold::destroyKey(path);
594 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700595 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700596 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700597 success &= android::vold::destroyKey(de_key_path);
598 } else {
599 LOG(INFO) << "Not present so not erasing: " << de_key_path;
600 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100601 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000602 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100603}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800604
Paul Crowley76107cb2016-02-09 10:04:39 +0000605static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700606 if (chmod(path.c_str(), 0000) != 0) {
607 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000608 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700609 }
610#if EMULATED_USES_SELINUX
611 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
612 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000613 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700614 }
615#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000616 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700617}
618
Paul Crowley76107cb2016-02-09 10:04:39 +0000619static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700620 if (chmod(path.c_str(), mode) != 0) {
621 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000622 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700623 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700624 }
625#if EMULATED_USES_SELINUX
626 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
627 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000628 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700629 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700630 }
631#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000632 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700633}
634
Paul Crowley3b71fc52017-10-09 10:55:21 -0700635static bool parse_hex(const std::string& hex, std::string* result) {
636 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800637 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000638 return true;
639 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800640 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800641 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000642 return false;
643 }
644 return true;
645}
646
Paul Crowley3aa914d2017-10-09 16:35:51 -0700647static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
648 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
649}
650
Paul Crowley26a53882017-10-26 11:16:39 -0700651static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
652 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
653}
654
Paul Crowley3aa914d2017-10-09 16:35:51 -0700655static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800656 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700657 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
658 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700659 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700660 if (android::vold::pathExists(secdiscardable_path)) {
661 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
662 return false;
663 } else {
664 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
665 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
666 return false;
667 }
668 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
669 return false;
670 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700671 auto key_path = volkey_path(misc_path, volume_uuid);
672 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
673 PLOG(ERROR) << "Creating directories for: " << key_path;
674 return false;
675 }
Paul Crowley26a53882017-10-26 11:16:39 -0700676 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700677
Steven Laverd4eb3412019-11-09 19:05:21 -0800678 if (!get_volume_file_encryption_options(&policy->options, flags)) return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700679 wrapped_key_supported = is_wrapped_key_supported_external(flags);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700680
Eric Biggers83a73d72019-09-30 13:06:47 -0700681 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley5e53ff62019-10-24 14:55:17 -0700682 volume_uuid, policy->options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800683 &policy->key_raw_ref, wrapped_key_supported);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700684}
685
686static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700687 auto path = volkey_path(misc_path, volume_uuid);
688 if (!android::vold::pathExists(path)) return true;
689 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700690}
691
Eric Biggersa701c452018-10-23 13:06:55 -0700692bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700693 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700694 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700695 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700696 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000697 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700698 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800699 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700700 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700701 auto auth =
702 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700703 auto const directory_path = get_ce_key_directory_path(user_id);
704 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700705
706 KeyBuffer ce_key;
707 if(is_wrapped_key_supported()) {
708 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
709 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
710 LOG(DEBUG) << "Successfully retrieved key";
711 } else {
712 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
713 LOG(DEBUG) << "Successfully retrieved key";
714 }
715 }
716 } else {
717 auto it = s_ce_keys.find(user_id);
718 if (it == s_ce_keys.end()) {
719 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
720 return false;
721 }
722 ce_key = it->second;
723 }
724
Paul Crowleya3630362016-05-17 14:17:56 -0700725 std::string ce_key_path;
726 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700727 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800728 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700729 return true;
730}
731
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800732bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700733 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800734 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700735 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800736 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700737 if (s_ephemeral_users.count(user_id) != 0) return true;
738 std::string token, secret;
739
740 if (!parse_hex(token_hex, &token)) return false;
741 if (!parse_hex(secret_hex, &secret)) return false;
742
743 if (is_wrapped_key_supported()) {
744 auto const directory_path = get_ce_key_directory_path(user_id);
745 auto const paths = get_ce_key_paths(directory_path);
746
747 KeyBuffer ce_key;
748 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
749
750 auto auth = android::vold::KeyAuthentication(token, secret);
751 /* Retrieve key while removing a pin. A secret is needed */
752 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
753 LOG(DEBUG) << "Successfully retrieved key";
754 } else {
755 /* Retrieve key when going None to swipe and vice versa when a
756 synthetic password is present */
757 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
758 LOG(DEBUG) << "Successfully retrieved key";
759 }
760 }
761
762 std::string ce_key_path;
763 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
764 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
765 return false;
766 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800767 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700768 }
769 return true;
770}
771
Eric Biggersa701c452018-10-23 13:06:55 -0700772bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
773 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
774 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700775 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700776 auto const directory_path = get_ce_key_directory_path(user_id);
777 auto const paths = get_ce_key_paths(directory_path);
778 if (paths.empty()) {
779 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
780 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000781 }
Paul Crowleya3630362016-05-17 14:17:56 -0700782 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000783 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000784}
785
Jeff Sharkey47695b22016-02-01 17:02:29 -0700786// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700787bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700788 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700789 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700790 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700791 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000792 if (s_ce_key_raw_refs.count(user_id) != 0) {
793 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000794 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000795 }
796 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800797 if (!parse_hex(token_hex, &token)) return false;
798 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000799 android::vold::KeyAuthentication auth(token, secret);
800 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000801 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000802 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800803 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700804 } else {
805 // When in emulation mode, we just use chmod. However, we also
806 // unlock directories when not in emulation mode, to bring devices
807 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000808 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800809 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700810 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
811 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700812 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000813 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700814 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800815 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000816 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800817}
818
Jeff Sharkey47695b22016-02-01 17:02:29 -0700819// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700820bool fscrypt_lock_user_key(userid_t user_id) {
821 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
822 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000823 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700824 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800825 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000826 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800827 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700828 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
829 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000830 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000831 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800832 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800833 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700834
Paul Crowley76107cb2016-02-09 10:04:39 +0000835 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800836}
837
Paul Crowley82b41ff2017-10-20 08:17:54 -0700838static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
839 userid_t user_id, int flags) {
840 if (0 != android::vold::ForkExecvp(
841 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
842 std::to_string(user_id), std::to_string(flags)})) {
843 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700844 return false;
845 }
846 return true;
847}
848
Eric Biggersa701c452018-10-23 13:06:55 -0700849bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700850 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700851 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800852 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700853
Paul Crowley82b41ff2017-10-20 08:17:54 -0700854 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600855 // DE_sys key
856 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
857 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
858 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600859
860 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700861 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
862 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800863 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700864 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
865
Paul Crowley3b71fc52017-10-09 10:55:21 -0700866 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700867 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600868#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700869 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700870 multiuser_get_uid(user_id, AID_EVERYBODY)))
871 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600872#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700873 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600874
Paul Crowley6b756ce2017-10-05 14:07:09 -0700875 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
876 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800877 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700878 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000879 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000880
Eric Biggersa701c452018-10-23 13:06:55 -0700881 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700882 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700883 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700884 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
885 return false;
886 get_data_file_encryption_options(&de_policy.options);
887 if (!EnsurePolicy(de_policy, system_de_path)) return false;
888 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
889 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700890 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800891 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700892 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700893 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700894 }
Paul Crowley285956f2016-01-20 13:12:38 +0000895 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800896
Paul Crowley82b41ff2017-10-20 08:17:54 -0700897 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600898 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700899 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
900 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800901 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600902 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
903 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800904
Paul Crowley3b71fc52017-10-09 10:55:21 -0700905 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700906 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
907 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800908 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700909 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000910 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
911 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700912
Eric Biggersa701c452018-10-23 13:06:55 -0700913 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700914 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700915 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700916 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
917 return false;
918 get_data_file_encryption_options(&ce_policy.options);
919 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
920 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
921 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700922
Paul Crowley3aa914d2017-10-09 16:35:51 -0700923 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800924 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700925 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700926 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
927 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700928 }
Paul Crowley1a965262017-10-13 13:49:50 -0700929
930 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700931 // Now that credentials have been installed, we can run restorecon
932 // over these paths
933 // NOTE: these paths need to be kept in sync with libselinux
934 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700935 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700936 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700937 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800938 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700939 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800940
Paul Crowley76107cb2016-02-09 10:04:39 +0000941 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800942}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600943
Eric Biggersa701c452018-10-23 13:06:55 -0700944bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
945 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600946 << ", user " << user_id << ", flags " << flags;
947 bool res = true;
948
Paul Crowley82b41ff2017-10-20 08:17:54 -0700949 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
950
951 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700952 // CE_n key
953 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
954 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800955 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700956 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
957 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
958
959 res &= destroy_dir(media_ce_path);
960 res &= destroy_dir(user_ce_path);
961 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700962 res &= destroy_dir(system_ce_path);
963 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800964 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700965 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700966 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700967 res &= destroy_volkey(misc_ce_path, volume_uuid);
968 }
Paul Crowley8e550662017-10-16 17:01:44 -0700969 }
970 }
971
Paul Crowley82b41ff2017-10-20 08:17:54 -0700972 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600973 // DE_sys key
974 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
975 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
976 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600977
978 // DE_n key
979 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
980 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800981 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600982 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
983
Paul Crowley8e550662017-10-16 17:01:44 -0700984 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700985 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600986 res &= destroy_dir(system_legacy_path);
987#if MANAGE_MISC_DIRS
988 res &= destroy_dir(misc_legacy_path);
989#endif
990 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600991 res &= destroy_dir(system_de_path);
992 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800993 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700994 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700995 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700996 res &= destroy_volkey(misc_de_path, volume_uuid);
997 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600998 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600999 }
1000
1001 return res;
1002}
Rubin Xu2436e272017-04-27 20:43:10 +01001003
Paul Crowleyc6433a22017-10-24 14:54:43 -07001004static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
1005 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
1006 if (!dirp) {
1007 PLOG(ERROR) << "Unable to open directory: " + directory_path;
1008 return false;
1009 }
1010 bool res = true;
1011 for (;;) {
1012 errno = 0;
1013 auto const entry = readdir(dirp.get());
1014 if (!entry) {
1015 if (errno) {
1016 PLOG(ERROR) << "Unable to read directory: " + directory_path;
1017 return false;
1018 }
1019 break;
1020 }
1021 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
1022 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
1023 continue;
1024 }
1025 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
1026 }
1027 return res;
1028}
1029
Eric Biggersa701c452018-10-23 13:06:55 -07001030bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -07001031 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -07001032 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -07001033 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
1034 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -07001035 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
1036 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
1037 return res;
1038}