blob: e31163fde1b1dda1f52ad4c327327dcc7759529a [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Paul Crowley1ef25582016-01-21 20:26:12 +000019#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070020#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080021#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070022#include "VoldUtil.h"
23
Paul Crowleya3630362016-05-17 14:17:56 -070024#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000025#include <map>
Barani Muthukumaranb1927c22019-10-31 22:59:34 -070026#include <optional>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000027#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070028#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080029#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070030#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000031
Paul Crowleydf528a72016-03-09 09:31:37 -080032#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070035#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070036#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080037#include <sys/mount.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070040#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000041
Paul Crowley480fcd22015-08-24 14:53:28 +010042#include <private/android_filesystem_config.h>
43
Paul Crowley82b41ff2017-10-20 08:17:54 -070044#include "android/os/IVold.h"
45
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070046#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060047#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070048
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080049#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070050#include <cutils/properties.h>
51
Eric Biggersa701c452018-10-23 13:06:55 -070052#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070053#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080054
Elliott Hughes7e128fb2015-12-04 15:50:53 -080055#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080056#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070057#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080058#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070059#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080060#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000061
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080062using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080063using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley77df7f22020-01-23 15:29:30 -080064using android::vold::BuildDataPath;
Paul Crowley05720802016-02-08 15:55:41 +000065using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010066using android::vold::KeyBuffer;
Paul Crowley249c2fb2020-02-07 12:51:56 -080067using android::vold::KeyGeneration;
Paul Crowley4eac2642020-02-12 11:04:05 -080068using android::vold::retrieveKey;
69using android::vold::retrieveOrGenerateKey;
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 Crowley77df7f22020-01-23 15:29:30 -080091// Map user ids to encryption policies
92std::map<userid_t, EncryptionPolicy> s_de_policies;
93std::map<userid_t, EncryptionPolicy> s_ce_policies;
Paul Lawrence731a7a22015-04-28 22:14:15 +000094
Paul Crowley14c8c072018-09-18 13:30:21 -070095} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000096
Paul Crowley249c2fb2020-02-07 12:51:56 -080097// Returns KeyGeneration suitable for key as described in EncryptionOptions
98static KeyGeneration makeGen(const EncryptionOptions& options) {
99 return KeyGeneration{FSCRYPT_MAX_KEY_SIZE, true, options.use_hw_wrapped_key};
100}
101
Eric Biggersa701c452018-10-23 13:06:55 -0700102static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000103 return property_get_bool("persist.sys.emulate_fbe", false);
104}
105
Paul Crowley3b71fc52017-10-09 10:55:21 -0700106static const char* escape_empty(const std::string& value) {
107 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000108}
109
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000110static std::string get_de_key_path(userid_t user_id) {
111 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
112}
113
Paul Crowleya3630362016-05-17 14:17:56 -0700114static std::string get_ce_key_directory_path(userid_t user_id) {
115 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
116}
117
118// Returns the keys newest first
119static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
120 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
121 if (!dirp) {
122 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
123 return std::vector<std::string>();
124 }
125 std::vector<std::string> result;
126 for (;;) {
127 errno = 0;
128 auto const entry = readdir(dirp.get());
129 if (!entry) {
130 if (errno) {
131 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
132 return std::vector<std::string>();
133 }
134 break;
135 }
136 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
137 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
138 continue;
139 }
140 result.emplace_back(directory_path + "/" + entry->d_name);
141 }
142 std::sort(result.begin(), result.end());
143 std::reverse(result.begin(), result.end());
144 return result;
145}
146
147static std::string get_ce_key_current_path(const std::string& directory_path) {
148 return directory_path + "/current";
149}
150
151static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700152 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700153 if (paths.empty()) {
154 *ce_key_path = get_ce_key_current_path(directory_path);
155 return true;
156 }
157 for (unsigned int i = 0; i < UINT_MAX; i++) {
158 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
159 if (paths[0] < candidate) {
160 *ce_key_path = candidate;
161 return true;
162 }
163 }
164 return false;
165}
166
167// Discard all keys but the named one; rename it to canonical name.
168// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700169static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700170 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700171 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700172 if (other_path != to_fix) {
173 android::vold::destroyKey(other_path);
174 }
175 }
176 auto const current_path = get_ce_key_current_path(directory_path);
177 if (to_fix != current_path) {
178 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
179 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
180 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800181 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700182 }
183 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800184 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700185}
186
187static bool read_and_fixate_user_ce_key(userid_t user_id,
188 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 auto const directory_path = get_ce_key_directory_path(user_id);
191 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700192 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700193 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
Paul Crowley4eac2642020-02-12 11:04:05 -0800194 if (retrieveKey(ce_key_path, auth, ce_key)) {
Paul Crowleya3630362016-05-17 14:17:56 -0700195 LOG(DEBUG) << "Successfully retrieved key";
196 fixate_user_ce_key(directory_path, ce_key_path, paths);
197 return true;
198 }
199 }
200 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
201 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100202}
203
Eric Biggers83a73d72019-09-30 13:06:47 -0700204// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley77df7f22020-01-23 15:29:30 -0800205static bool get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700206 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
207 if (entry == nullptr) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800208 LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
209 return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700210 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700211 if (!ParseOptions(entry->encryption_options, options)) {
212 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
213 << entry->encryption_options;
Paul Crowley77df7f22020-01-23 15:29:30 -0800214 return false;
Paul Crowleya50f6c32019-10-24 23:21:44 -0700215 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800216 return true;
Eric Biggers83a73d72019-09-30 13:06:47 -0700217}
218
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800219static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options,
220 const KeyBuffer& key, EncryptionPolicy* policy) {
221 KeyBuffer ephemeral_wrapped_key;
222 if (options.use_hw_wrapped_key) {
223 if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) {
224 LOG(ERROR) << "Failed to get ephemeral wrapped key";
225 return false;
226 }
227 }
228 return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key,
229 policy);
230}
231
Eric Biggers83a73d72019-09-30 13:06:47 -0700232// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700233static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyeb241a12020-02-18 10:10:08 -0800234 // If we give the empty string, libfscrypt will use the default (currently XTS)
235 auto contents_mode = android::base::GetProperty("ro.crypto.volume.contents_mode", "");
236 // HEH as default was always a mistake. Use the libfscrypt default (CTS)
237 // for devices launching on versions above Android 10.
238 auto first_api_level = GetFirstApiLevel();
239 constexpr uint64_t pre_gki_level = 29;
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700240 auto filenames_mode =
Paul Crowleyeb241a12020-02-18 10:10:08 -0800241 android::base::GetProperty("ro.crypto.volume.filenames_mode",
242 first_api_level > pre_gki_level ? "" : "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800243 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
Paul Crowleyeb241a12020-02-18 10:10:08 -0800244 contents_mode + ":" + filenames_mode);
245 if (!ParseOptionsForApiLevel(first_api_level, options_string, options)) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800246 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
247 return false;
248 }
249 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700250}
251
Paul Crowleydf528a72016-03-09 09:31:37 -0800252static bool read_and_install_user_ce_key(userid_t user_id,
253 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800254 if (s_ce_policies.count(user_id) != 0) return true;
255 EncryptionOptions options;
256 if (!get_data_file_encryption_options(&options)) return false;
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 Crowley77df7f22020-01-23 15:29:30 -0800259 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800260 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800261 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000262 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000263 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000264}
265
Paul Crowleydf528a72016-03-09 09:31:37 -0800266static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000267 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000268 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
269 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000270 return false;
271 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000272 return true;
273}
274
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600275static bool destroy_dir(const std::string& dir) {
276 LOG(DEBUG) << "Destroying: " << dir;
277 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
278 PLOG(ERROR) << "Failed to destroy " << dir;
279 return false;
280 }
281 return true;
282}
283
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000284// NB this assumes that there is only one thread listening for crypt commands, because
285// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000286static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800287 EncryptionOptions options;
288 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100289 KeyBuffer de_key, ce_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800290 if (!generateStorageKey(makeGen(options), &de_key)) return false;
291 if (!generateStorageKey(makeGen(options), &ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000292 if (create_ephemeral) {
293 // If the key should be created as ephemeral, don't store it.
294 s_ephemeral_users.insert(user_id);
295 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700296 auto const directory_path = get_ce_key_directory_path(user_id);
297 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
298 auto const paths = get_ce_key_paths(directory_path);
299 std::string ce_key_path;
300 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700301 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
302 ce_key))
303 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700304 fixate_user_ce_key(directory_path, ce_key_path, paths);
305 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700306 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700307 kEmptyAuthentication, de_key))
308 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100309 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800310 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800311 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800312 s_de_policies[user_id] = de_policy;
313 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800314 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800315 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000316 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000317 return true;
318}
319
Paul Crowley77df7f22020-01-23 15:29:30 -0800320static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
321 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000322 auto refi = key_map.find(user_id);
323 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700324 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000325 return false;
326 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800327 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000328 return true;
329}
330
Paul Crowleydf528a72016-03-09 09:31:37 -0800331static bool is_numeric(const char* name) {
332 for (const char* p = name; *p != '\0'; p++) {
333 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000334 }
335 return true;
336}
337
338static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800339 EncryptionOptions options;
340 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000341 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800342 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000343 if (!dirp) {
344 PLOG(ERROR) << "Unable to read de key directory";
345 return false;
346 }
347 for (;;) {
348 errno = 0;
349 auto entry = readdir(dirp.get());
350 if (!entry) {
351 if (errno) {
352 PLOG(ERROR) << "Unable to read de key directory";
353 return false;
354 }
355 break;
356 }
357 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
358 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
359 continue;
360 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600361 userid_t user_id = std::stoi(entry->d_name);
Paul Crowley77df7f22020-01-23 15:29:30 -0800362 if (s_de_policies.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000363 auto key_path = de_dir + "/" + entry->d_name;
Paul Crowley77df7f22020-01-23 15:29:30 -0800364 KeyBuffer de_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800365 if (!retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800366 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800367 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800368 s_de_policies[user_id] = de_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000369 LOG(DEBUG) << "Installed de key for user " << user_id;
370 }
371 }
Eric Biggersa701c452018-10-23 13:06:55 -0700372 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000373 // correct policy set on them, and that no rogue ones exist.
374 return true;
375}
376
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700377bool fscrypt_initialize_systemwide_keys() {
378 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800379
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700380 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000381 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000382 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800383 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800384 EncryptionOptions options;
385 if (!get_data_file_encryption_options(&options)) return false;
386
387 KeyBuffer device_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800388 if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
389 makeGen(options), &device_key))
Paul Crowley77df7f22020-01-23 15:29:30 -0800390 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800391
Paul Crowley5e53ff62019-10-24 14:55:17 -0700392 EncryptionPolicy device_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800393 if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700394
Paul Crowley5e53ff62019-10-24 14:55:17 -0700395 std::string options_string;
396 if (!OptionsToString(device_policy.options, &options_string)) {
397 LOG(ERROR) << "Unable to serialize options";
398 return false;
399 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800400 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700401 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700402
Paul Crowley77df7f22020-01-23 15:29:30 -0800403 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700404 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700405 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800406
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700407 KeyBuffer per_boot_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800408 if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800409 EncryptionPolicy per_boot_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800410 if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700411 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800412 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
413 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700414 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
415
Tommy Chiua98464f2019-03-26 14:14:19 +0800416 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700417 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000418 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800419}
420
Eric Biggersa701c452018-10-23 13:06:55 -0700421bool fscrypt_init_user0() {
422 LOG(DEBUG) << "fscrypt_init_user0";
423 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000424 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
425 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
426 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700427 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000428 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000429 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700430 // TODO: switch to loading only DE_0 here once framework makes
431 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000432 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000433 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700434 // We can only safely prepare DE storage here, since CE keys are probably
435 // entangled with user credentials. The framework will always prepare CE
436 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700437 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700438 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000439 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700440 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700441
442 // If this is a non-FBE device that recently left an emulated mode,
443 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700444 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
445 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700446 }
447
Paul Crowley76107cb2016-02-09 10:04:39 +0000448 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000449}
450
Eric Biggersa701c452018-10-23 13:06:55 -0700451bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
452 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
453 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000454 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000455 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000456 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800457 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700458 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800459 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000460 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000461 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800462 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000463 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000464 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000465 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000466 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800467}
468
Eric Biggersce368682019-04-03 15:44:06 -0700469// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700470// in the case where the keys are being put in the session keyring (rather in
471// the newer filesystem-level keyrings), because removing a key from the session
472// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
473// was already set up. So to remove the per-file keys and make the files
474// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700475//
476// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
477// objects systemwide. This is overkill, but it's the best available method
478// currently. Don't use drop_caches mode "3" because that also evicts pagecache
479// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700480static void drop_caches_if_needed() {
481 if (android::vold::isFsKeyringSupported()) {
482 return;
483 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100484 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700485 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100486 PLOG(ERROR) << "Failed to drop caches during key eviction";
487 }
488}
489
Andrew Scull7ec25c72016-10-31 10:28:25 +0000490static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000491 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800492 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000493 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800494 if (lookup_policy(s_ce_policies, user_id, &policy)) {
495 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700496 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000497 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800498 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000499 return success;
500}
501
Eric Biggersa701c452018-10-23 13:06:55 -0700502bool fscrypt_destroy_user_key(userid_t user_id) {
503 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
504 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000505 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000506 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000507 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000508 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800509 EncryptionPolicy de_policy;
510 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
511 android::vold::evictKey(DATA_MNT_POINT, de_policy);
512 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000513 auto it = s_ephemeral_users.find(user_id);
514 if (it != s_ephemeral_users.end()) {
515 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000516 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700517 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700518 success &= android::vold::destroyKey(path);
519 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700520 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700521 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700522 success &= android::vold::destroyKey(de_key_path);
523 } else {
524 LOG(INFO) << "Not present so not erasing: " << de_key_path;
525 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100526 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000527 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100528}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800529
Paul Crowley76107cb2016-02-09 10:04:39 +0000530static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700531 if (chmod(path.c_str(), 0000) != 0) {
532 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000533 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700534 }
535#if EMULATED_USES_SELINUX
536 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
537 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000538 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700539 }
540#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000541 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700542}
543
Paul Crowley76107cb2016-02-09 10:04:39 +0000544static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700545 if (chmod(path.c_str(), mode) != 0) {
546 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000547 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700548 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700549 }
550#if EMULATED_USES_SELINUX
551 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
552 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000553 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700554 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700555 }
556#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000557 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700558}
559
Paul Crowley3b71fc52017-10-09 10:55:21 -0700560static bool parse_hex(const std::string& hex, std::string* result) {
561 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800562 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000563 return true;
564 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800565 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800566 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000567 return false;
568 }
569 return true;
570}
571
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700572static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
573 const std::string& token_hex, const std::string& secret_hex) {
574 std::string token, secret;
575 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
576 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
577 if (secret.empty()) {
578 return kEmptyAuthentication;
579 } else {
580 return android::vold::KeyAuthentication(token, secret);
581 }
582}
583
Paul Crowley3aa914d2017-10-09 16:35:51 -0700584static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
585 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
586}
587
Paul Crowley26a53882017-10-26 11:16:39 -0700588static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
589 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
590}
591
Paul Crowley3aa914d2017-10-09 16:35:51 -0700592static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700593 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700594 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
595 std::string secdiscardable_hash;
596 if (android::vold::pathExists(secdiscardable_path)) {
597 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
598 return false;
599 } else {
600 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
601 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
602 return false;
603 }
604 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
605 return false;
606 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700607 auto key_path = volkey_path(misc_path, volume_uuid);
608 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
609 PLOG(ERROR) << "Creating directories for: " << key_path;
610 return false;
611 }
Paul Crowley26a53882017-10-26 11:16:39 -0700612 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700613
Paul Crowley77df7f22020-01-23 15:29:30 -0800614 EncryptionOptions options;
615 if (!get_volume_file_encryption_options(&options)) return false;
616 KeyBuffer key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800617 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800618 return false;
619 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800620 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700621}
622
623static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700624 auto path = volkey_path(misc_path, volume_uuid);
625 if (!android::vold::pathExists(path)) return true;
626 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700627}
628
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700629static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
630 const android::vold::KeyAuthentication& retrieve_auth,
631 const android::vold::KeyAuthentication& store_auth) {
632 if (s_ephemeral_users.count(user_id) != 0) return true;
633 auto const directory_path = get_ce_key_directory_path(user_id);
634 KeyBuffer ce_key;
635 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Paul Crowley4eac2642020-02-12 11:04:05 -0800636 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700637 LOG(DEBUG) << "Successfully retrieved key";
638 // TODO(147732812): Remove this once Locksettingservice is fixed.
639 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
640 // changed from swipe to none or vice-versa
Paul Crowley4eac2642020-02-12 11:04:05 -0800641 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700642 LOG(DEBUG) << "Successfully retrieved key with empty auth";
643 } else {
644 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
645 return false;
646 }
647 auto const paths = get_ce_key_paths(directory_path);
648 std::string ce_key_path;
649 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
650 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
651 return false;
652 if (!android::vold::FsyncDirectory(directory_path)) return false;
653 return true;
654}
655
Eric Biggersa701c452018-10-23 13:06:55 -0700656bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700657 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700658 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700659 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700660 if (!fscrypt_is_native()) return true;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700661 auto auth = authentication_from_hex(token_hex, secret_hex);
662 if (!auth) return false;
663 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
664}
665
666bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
667 const std::string& secret_hex) {
668 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
669 << " token_present=" << (token_hex != "!");
670 if (!fscrypt_is_native()) return true;
671 auto auth = authentication_from_hex(token_hex, secret_hex);
672 if (!auth) return false;
673 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Paul Crowleya3630362016-05-17 14:17:56 -0700674}
675
Eric Biggersa701c452018-10-23 13:06:55 -0700676bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
677 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
678 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700679 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700680 auto const directory_path = get_ce_key_directory_path(user_id);
681 auto const paths = get_ce_key_paths(directory_path);
682 if (paths.empty()) {
683 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
684 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000685 }
Paul Crowleya3630362016-05-17 14:17:56 -0700686 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000687 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000688}
689
Jeff Sharkey47695b22016-02-01 17:02:29 -0700690// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700691bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700692 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700693 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700694 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700695 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800696 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000697 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000698 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000699 }
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700700 auto auth = authentication_from_hex(token_hex, secret_hex);
701 if (!auth) return false;
702 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000703 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000704 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800705 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700706 } else {
707 // When in emulation mode, we just use chmod. However, we also
708 // unlock directories when not in emulation mode, to bring devices
709 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000710 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800711 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700712 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
713 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700714 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000715 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700716 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800717 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000718 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800719}
720
Jeff Sharkey47695b22016-02-01 17:02:29 -0700721// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700722bool fscrypt_lock_user_key(userid_t user_id) {
723 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
724 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000725 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700726 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800727 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000728 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800729 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700730 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
731 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000732 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000733 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800734 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800735 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700736
Paul Crowley76107cb2016-02-09 10:04:39 +0000737 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800738}
739
Paul Crowley82b41ff2017-10-20 08:17:54 -0700740static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
741 userid_t user_id, int flags) {
742 if (0 != android::vold::ForkExecvp(
743 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
744 std::to_string(user_id), std::to_string(flags)})) {
745 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700746 return false;
747 }
748 return true;
749}
750
Eric Biggersa701c452018-10-23 13:06:55 -0700751bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700752 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700753 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800754 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700755
Paul Crowley82b41ff2017-10-20 08:17:54 -0700756 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600757 // DE_sys key
758 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
759 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
760 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600761
762 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700763 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
764 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800765 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700766 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
767
Paul Crowley3b71fc52017-10-09 10:55:21 -0700768 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700769 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600770#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700771 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700772 multiuser_get_uid(user_id, AID_EVERYBODY)))
773 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600774#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700775 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600776
Paul Crowley6b756ce2017-10-05 14:07:09 -0700777 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
778 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800779 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700780 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000781 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000782
Eric Biggersa701c452018-10-23 13:06:55 -0700783 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700784 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700785 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800786 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700787 if (!EnsurePolicy(de_policy, system_de_path)) return false;
788 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
789 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700790 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700791 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700792 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700793 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700794 }
Paul Crowley285956f2016-01-20 13:12:38 +0000795 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800796
Paul Crowley82b41ff2017-10-20 08:17:54 -0700797 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600798 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700799 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
800 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800801 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600802 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
803 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800804
Paul Crowley3b71fc52017-10-09 10:55:21 -0700805 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700806 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
807 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800808 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700809 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000810 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
811 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700812
Eric Biggersa701c452018-10-23 13:06:55 -0700813 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700814 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700815 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800816 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700817 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
818 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
819 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700820 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700821 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700822 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700823 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
824 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700825 }
Paul Crowley1a965262017-10-13 13:49:50 -0700826
827 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700828 // Now that credentials have been installed, we can run restorecon
829 // over these paths
830 // NOTE: these paths need to be kept in sync with libselinux
831 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700832 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700833 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700834 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800835 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700836 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800837
Paul Crowley76107cb2016-02-09 10:04:39 +0000838 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800839}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600840
Eric Biggersa701c452018-10-23 13:06:55 -0700841bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
842 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600843 << ", user " << user_id << ", flags " << flags;
844 bool res = true;
845
Paul Crowley82b41ff2017-10-20 08:17:54 -0700846 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
847
848 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700849 // CE_n key
850 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
851 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800852 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700853 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
854 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
855
856 res &= destroy_dir(media_ce_path);
857 res &= destroy_dir(user_ce_path);
858 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700859 res &= destroy_dir(system_ce_path);
860 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800861 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700862 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700863 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700864 res &= destroy_volkey(misc_ce_path, volume_uuid);
865 }
Paul Crowley8e550662017-10-16 17:01:44 -0700866 }
867 }
868
Paul Crowley82b41ff2017-10-20 08:17:54 -0700869 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600870 // DE_sys key
871 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
872 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
873 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600874
875 // DE_n key
876 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
877 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800878 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600879 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
880
Paul Crowley8e550662017-10-16 17:01:44 -0700881 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700882 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600883 res &= destroy_dir(system_legacy_path);
884#if MANAGE_MISC_DIRS
885 res &= destroy_dir(misc_legacy_path);
886#endif
887 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600888 res &= destroy_dir(system_de_path);
889 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800890 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700891 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700892 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700893 res &= destroy_volkey(misc_de_path, volume_uuid);
894 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600895 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600896 }
897
898 return res;
899}
Rubin Xu2436e272017-04-27 20:43:10 +0100900
Paul Crowleyc6433a22017-10-24 14:54:43 -0700901static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
902 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
903 if (!dirp) {
904 PLOG(ERROR) << "Unable to open directory: " + directory_path;
905 return false;
906 }
907 bool res = true;
908 for (;;) {
909 errno = 0;
910 auto const entry = readdir(dirp.get());
911 if (!entry) {
912 if (errno) {
913 PLOG(ERROR) << "Unable to read directory: " + directory_path;
914 return false;
915 }
916 break;
917 }
918 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
919 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
920 continue;
921 }
922 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
923 }
924 return res;
925}
926
Eric Biggersa701c452018-10-23 13:06:55 -0700927bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700928 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700929 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700930 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
931 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700932 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
933 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
934 return res;
935}