blob: a01ed5ed6021f607d570765461ac558ccd934596 [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 Crowleyf612b8b2019-10-24 22:52:02 -0700234 auto contents_mode =
235 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
236 auto filenames_mode =
237 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800238 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
239 contents_mode + ":" + filenames_mode + ":v1");
240 if (!ParseOptions(options_string, options)) {
241 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
242 return false;
243 }
244 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700245}
246
Paul Crowleydf528a72016-03-09 09:31:37 -0800247static bool read_and_install_user_ce_key(userid_t user_id,
248 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800249 if (s_ce_policies.count(user_id) != 0) return true;
250 EncryptionOptions options;
251 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100252 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700253 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800254 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800255 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800256 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000257 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000258 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000259}
260
Paul Crowleydf528a72016-03-09 09:31:37 -0800261static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000262 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000263 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
264 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000265 return false;
266 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000267 return true;
268}
269
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600270static bool destroy_dir(const std::string& dir) {
271 LOG(DEBUG) << "Destroying: " << dir;
272 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
273 PLOG(ERROR) << "Failed to destroy " << dir;
274 return false;
275 }
276 return true;
277}
278
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000279// NB this assumes that there is only one thread listening for crypt commands, because
280// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000281static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800282 EncryptionOptions options;
283 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100284 KeyBuffer de_key, ce_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800285 if (!generateStorageKey(makeGen(options), &de_key)) return false;
286 if (!generateStorageKey(makeGen(options), &ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000287 if (create_ephemeral) {
288 // If the key should be created as ephemeral, don't store it.
289 s_ephemeral_users.insert(user_id);
290 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700291 auto const directory_path = get_ce_key_directory_path(user_id);
292 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
293 auto const paths = get_ce_key_paths(directory_path);
294 std::string ce_key_path;
295 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700296 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
297 ce_key))
298 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700299 fixate_user_ce_key(directory_path, ce_key_path, paths);
300 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700301 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700302 kEmptyAuthentication, de_key))
303 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100304 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800305 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800306 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800307 s_de_policies[user_id] = de_policy;
308 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800309 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800310 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000311 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000312 return true;
313}
314
Paul Crowley77df7f22020-01-23 15:29:30 -0800315static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
316 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000317 auto refi = key_map.find(user_id);
318 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700319 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000320 return false;
321 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800322 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000323 return true;
324}
325
Paul Crowleydf528a72016-03-09 09:31:37 -0800326static bool is_numeric(const char* name) {
327 for (const char* p = name; *p != '\0'; p++) {
328 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000329 }
330 return true;
331}
332
333static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800334 EncryptionOptions options;
335 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000336 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800337 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000338 if (!dirp) {
339 PLOG(ERROR) << "Unable to read de key directory";
340 return false;
341 }
342 for (;;) {
343 errno = 0;
344 auto entry = readdir(dirp.get());
345 if (!entry) {
346 if (errno) {
347 PLOG(ERROR) << "Unable to read de key directory";
348 return false;
349 }
350 break;
351 }
352 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
353 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
354 continue;
355 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600356 userid_t user_id = std::stoi(entry->d_name);
Paul Crowley77df7f22020-01-23 15:29:30 -0800357 if (s_de_policies.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000358 auto key_path = de_dir + "/" + entry->d_name;
Paul Crowley77df7f22020-01-23 15:29:30 -0800359 KeyBuffer de_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800360 if (!retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800361 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800362 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800363 s_de_policies[user_id] = de_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000364 LOG(DEBUG) << "Installed de key for user " << user_id;
365 }
366 }
Eric Biggersa701c452018-10-23 13:06:55 -0700367 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000368 // correct policy set on them, and that no rogue ones exist.
369 return true;
370}
371
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700372bool fscrypt_initialize_systemwide_keys() {
373 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800374
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700375 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000376 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000377 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800378 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800379 EncryptionOptions options;
380 if (!get_data_file_encryption_options(&options)) return false;
381
382 KeyBuffer device_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800383 if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
384 makeGen(options), &device_key))
Paul Crowley77df7f22020-01-23 15:29:30 -0800385 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800386
Paul Crowley5e53ff62019-10-24 14:55:17 -0700387 EncryptionPolicy device_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800388 if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700389
Paul Crowley5e53ff62019-10-24 14:55:17 -0700390 std::string options_string;
391 if (!OptionsToString(device_policy.options, &options_string)) {
392 LOG(ERROR) << "Unable to serialize options";
393 return false;
394 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800395 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700396 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700397
Paul Crowley77df7f22020-01-23 15:29:30 -0800398 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700399 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700400 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800401
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700402 KeyBuffer per_boot_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800403 if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800404 EncryptionPolicy per_boot_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800405 if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700406 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800407 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
408 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700409 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
410
Tommy Chiua98464f2019-03-26 14:14:19 +0800411 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700412 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000413 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800414}
415
Eric Biggersa701c452018-10-23 13:06:55 -0700416bool fscrypt_init_user0() {
417 LOG(DEBUG) << "fscrypt_init_user0";
418 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000419 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
420 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
421 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700422 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000423 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000424 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700425 // TODO: switch to loading only DE_0 here once framework makes
426 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000427 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000428 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700429 // We can only safely prepare DE storage here, since CE keys are probably
430 // entangled with user credentials. The framework will always prepare CE
431 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700432 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700433 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000434 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700435 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700436
437 // If this is a non-FBE device that recently left an emulated mode,
438 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700439 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
440 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700441 }
442
Paul Crowley76107cb2016-02-09 10:04:39 +0000443 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000444}
445
Eric Biggersa701c452018-10-23 13:06:55 -0700446bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
447 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
448 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000449 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000450 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000451 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800452 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700453 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800454 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000455 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000456 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800457 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000458 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000459 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000460 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000461 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800462}
463
Eric Biggersce368682019-04-03 15:44:06 -0700464// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700465// in the case where the keys are being put in the session keyring (rather in
466// the newer filesystem-level keyrings), because removing a key from the session
467// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
468// was already set up. So to remove the per-file keys and make the files
469// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700470//
471// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
472// objects systemwide. This is overkill, but it's the best available method
473// currently. Don't use drop_caches mode "3" because that also evicts pagecache
474// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700475static void drop_caches_if_needed() {
476 if (android::vold::isFsKeyringSupported()) {
477 return;
478 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100479 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700480 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100481 PLOG(ERROR) << "Failed to drop caches during key eviction";
482 }
483}
484
Andrew Scull7ec25c72016-10-31 10:28:25 +0000485static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000486 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800487 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000488 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800489 if (lookup_policy(s_ce_policies, user_id, &policy)) {
490 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700491 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000492 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800493 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000494 return success;
495}
496
Eric Biggersa701c452018-10-23 13:06:55 -0700497bool fscrypt_destroy_user_key(userid_t user_id) {
498 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
499 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000500 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000501 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000502 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000503 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800504 EncryptionPolicy de_policy;
505 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
506 android::vold::evictKey(DATA_MNT_POINT, de_policy);
507 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000508 auto it = s_ephemeral_users.find(user_id);
509 if (it != s_ephemeral_users.end()) {
510 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000511 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700512 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700513 success &= android::vold::destroyKey(path);
514 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700515 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700516 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700517 success &= android::vold::destroyKey(de_key_path);
518 } else {
519 LOG(INFO) << "Not present so not erasing: " << de_key_path;
520 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100521 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000522 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100523}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800524
Paul Crowley76107cb2016-02-09 10:04:39 +0000525static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700526 if (chmod(path.c_str(), 0000) != 0) {
527 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000528 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700529 }
530#if EMULATED_USES_SELINUX
531 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
532 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000533 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700534 }
535#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000536 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700537}
538
Paul Crowley76107cb2016-02-09 10:04:39 +0000539static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700540 if (chmod(path.c_str(), mode) != 0) {
541 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000542 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700543 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700544 }
545#if EMULATED_USES_SELINUX
546 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
547 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000548 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700549 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700550 }
551#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000552 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700553}
554
Paul Crowley3b71fc52017-10-09 10:55:21 -0700555static bool parse_hex(const std::string& hex, std::string* result) {
556 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800557 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000558 return true;
559 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800560 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800561 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000562 return false;
563 }
564 return true;
565}
566
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700567static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
568 const std::string& token_hex, const std::string& secret_hex) {
569 std::string token, secret;
570 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
571 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
572 if (secret.empty()) {
573 return kEmptyAuthentication;
574 } else {
575 return android::vold::KeyAuthentication(token, secret);
576 }
577}
578
Paul Crowley3aa914d2017-10-09 16:35:51 -0700579static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
580 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
581}
582
Paul Crowley26a53882017-10-26 11:16:39 -0700583static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
584 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
585}
586
Paul Crowley3aa914d2017-10-09 16:35:51 -0700587static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700588 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700589 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
590 std::string secdiscardable_hash;
591 if (android::vold::pathExists(secdiscardable_path)) {
592 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
593 return false;
594 } else {
595 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
596 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
597 return false;
598 }
599 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
600 return false;
601 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700602 auto key_path = volkey_path(misc_path, volume_uuid);
603 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
604 PLOG(ERROR) << "Creating directories for: " << key_path;
605 return false;
606 }
Paul Crowley26a53882017-10-26 11:16:39 -0700607 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700608
Paul Crowley77df7f22020-01-23 15:29:30 -0800609 EncryptionOptions options;
610 if (!get_volume_file_encryption_options(&options)) return false;
611 KeyBuffer key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800612 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800613 return false;
614 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800615 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700616}
617
618static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700619 auto path = volkey_path(misc_path, volume_uuid);
620 if (!android::vold::pathExists(path)) return true;
621 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700622}
623
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700624static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
625 const android::vold::KeyAuthentication& retrieve_auth,
626 const android::vold::KeyAuthentication& store_auth) {
627 if (s_ephemeral_users.count(user_id) != 0) return true;
628 auto const directory_path = get_ce_key_directory_path(user_id);
629 KeyBuffer ce_key;
630 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Paul Crowley4eac2642020-02-12 11:04:05 -0800631 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700632 LOG(DEBUG) << "Successfully retrieved key";
633 // TODO(147732812): Remove this once Locksettingservice is fixed.
634 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
635 // changed from swipe to none or vice-versa
Paul Crowley4eac2642020-02-12 11:04:05 -0800636 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700637 LOG(DEBUG) << "Successfully retrieved key with empty auth";
638 } else {
639 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
640 return false;
641 }
642 auto const paths = get_ce_key_paths(directory_path);
643 std::string ce_key_path;
644 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
645 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
646 return false;
647 if (!android::vold::FsyncDirectory(directory_path)) return false;
648 return true;
649}
650
Eric Biggersa701c452018-10-23 13:06:55 -0700651bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700652 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700653 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700654 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700655 if (!fscrypt_is_native()) return true;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700656 auto auth = authentication_from_hex(token_hex, secret_hex);
657 if (!auth) return false;
658 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
659}
660
661bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
662 const std::string& secret_hex) {
663 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
664 << " token_present=" << (token_hex != "!");
665 if (!fscrypt_is_native()) return true;
666 auto auth = authentication_from_hex(token_hex, secret_hex);
667 if (!auth) return false;
668 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Paul Crowleya3630362016-05-17 14:17:56 -0700669}
670
Eric Biggersa701c452018-10-23 13:06:55 -0700671bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
672 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
673 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700674 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700675 auto const directory_path = get_ce_key_directory_path(user_id);
676 auto const paths = get_ce_key_paths(directory_path);
677 if (paths.empty()) {
678 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
679 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000680 }
Paul Crowleya3630362016-05-17 14:17:56 -0700681 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000682 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000683}
684
Jeff Sharkey47695b22016-02-01 17:02:29 -0700685// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700686bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700687 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700688 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700689 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700690 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800691 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000692 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000693 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000694 }
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700695 auto auth = authentication_from_hex(token_hex, secret_hex);
696 if (!auth) return false;
697 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000698 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000699 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800700 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700701 } else {
702 // When in emulation mode, we just use chmod. However, we also
703 // unlock directories when not in emulation mode, to bring devices
704 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000705 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800706 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700707 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
708 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700709 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000710 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700711 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800712 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000713 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800714}
715
Jeff Sharkey47695b22016-02-01 17:02:29 -0700716// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700717bool fscrypt_lock_user_key(userid_t user_id) {
718 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
719 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000720 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700721 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800722 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000723 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800724 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700725 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
726 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000727 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000728 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800729 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800730 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700731
Paul Crowley76107cb2016-02-09 10:04:39 +0000732 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800733}
734
Paul Crowley82b41ff2017-10-20 08:17:54 -0700735static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
736 userid_t user_id, int flags) {
737 if (0 != android::vold::ForkExecvp(
738 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
739 std::to_string(user_id), std::to_string(flags)})) {
740 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700741 return false;
742 }
743 return true;
744}
745
Eric Biggersa701c452018-10-23 13:06:55 -0700746bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700747 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700748 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800749 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700750
Paul Crowley82b41ff2017-10-20 08:17:54 -0700751 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600752 // DE_sys key
753 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
754 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
755 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600756
757 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700758 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
759 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800760 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700761 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
762
Paul Crowley3b71fc52017-10-09 10:55:21 -0700763 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700764 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600765#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700766 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700767 multiuser_get_uid(user_id, AID_EVERYBODY)))
768 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600769#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700770 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600771
Paul Crowley6b756ce2017-10-05 14:07:09 -0700772 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
773 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800774 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700775 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000776 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000777
Eric Biggersa701c452018-10-23 13:06:55 -0700778 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700779 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700780 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800781 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700782 if (!EnsurePolicy(de_policy, system_de_path)) return false;
783 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
784 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700785 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700786 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700787 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700788 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700789 }
Paul Crowley285956f2016-01-20 13:12:38 +0000790 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800791
Paul Crowley82b41ff2017-10-20 08:17:54 -0700792 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600793 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700794 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
795 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800796 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600797 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
798 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800799
Paul Crowley3b71fc52017-10-09 10:55:21 -0700800 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700801 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
802 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800803 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700804 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000805 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
806 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700807
Eric Biggersa701c452018-10-23 13:06:55 -0700808 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700809 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700810 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800811 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700812 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
813 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
814 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700815 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700816 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700817 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700818 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
819 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700820 }
Paul Crowley1a965262017-10-13 13:49:50 -0700821
822 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700823 // Now that credentials have been installed, we can run restorecon
824 // over these paths
825 // NOTE: these paths need to be kept in sync with libselinux
826 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700827 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700828 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700829 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800830 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700831 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800832
Paul Crowley76107cb2016-02-09 10:04:39 +0000833 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800834}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600835
Eric Biggersa701c452018-10-23 13:06:55 -0700836bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
837 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600838 << ", user " << user_id << ", flags " << flags;
839 bool res = true;
840
Paul Crowley82b41ff2017-10-20 08:17:54 -0700841 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
842
843 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700844 // CE_n key
845 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
846 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800847 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700848 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
849 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
850
851 res &= destroy_dir(media_ce_path);
852 res &= destroy_dir(user_ce_path);
853 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700854 res &= destroy_dir(system_ce_path);
855 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800856 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700857 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700858 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700859 res &= destroy_volkey(misc_ce_path, volume_uuid);
860 }
Paul Crowley8e550662017-10-16 17:01:44 -0700861 }
862 }
863
Paul Crowley82b41ff2017-10-20 08:17:54 -0700864 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600865 // DE_sys key
866 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
867 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
868 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600869
870 // DE_n key
871 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
872 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800873 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600874 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
875
Paul Crowley8e550662017-10-16 17:01:44 -0700876 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700877 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600878 res &= destroy_dir(system_legacy_path);
879#if MANAGE_MISC_DIRS
880 res &= destroy_dir(misc_legacy_path);
881#endif
882 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600883 res &= destroy_dir(system_de_path);
884 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800885 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700886 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700887 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700888 res &= destroy_volkey(misc_de_path, volume_uuid);
889 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600890 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600891 }
892
893 return res;
894}
Rubin Xu2436e272017-04-27 20:43:10 +0100895
Paul Crowleyc6433a22017-10-24 14:54:43 -0700896static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
897 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
898 if (!dirp) {
899 PLOG(ERROR) << "Unable to open directory: " + directory_path;
900 return false;
901 }
902 bool res = true;
903 for (;;) {
904 errno = 0;
905 auto const entry = readdir(dirp.get());
906 if (!entry) {
907 if (errno) {
908 PLOG(ERROR) << "Unable to read directory: " + directory_path;
909 return false;
910 }
911 break;
912 }
913 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
914 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
915 continue;
916 }
917 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
918 }
919 return res;
920}
921
Eric Biggersa701c452018-10-23 13:06:55 -0700922bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700923 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700924 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700925 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
926 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700927 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
928 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
929 return res;
930}