blob: c0ec3eb8488bc32c0f2c9986182802b66884a79e [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 Crowley4eac2642020-02-12 11:04:05 -080067using android::vold::makeGen;
68using 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
Eric Biggersa701c452018-10-23 13:06:55 -070097static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +000098 return property_get_bool("persist.sys.emulate_fbe", false);
99}
100
Paul Crowley3b71fc52017-10-09 10:55:21 -0700101static const char* escape_empty(const std::string& value) {
102 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000103}
104
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000105static std::string get_de_key_path(userid_t user_id) {
106 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
107}
108
Paul Crowleya3630362016-05-17 14:17:56 -0700109static std::string get_ce_key_directory_path(userid_t user_id) {
110 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
111}
112
113// Returns the keys newest first
114static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
115 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
116 if (!dirp) {
117 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
118 return std::vector<std::string>();
119 }
120 std::vector<std::string> result;
121 for (;;) {
122 errno = 0;
123 auto const entry = readdir(dirp.get());
124 if (!entry) {
125 if (errno) {
126 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
127 return std::vector<std::string>();
128 }
129 break;
130 }
131 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
132 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
133 continue;
134 }
135 result.emplace_back(directory_path + "/" + entry->d_name);
136 }
137 std::sort(result.begin(), result.end());
138 std::reverse(result.begin(), result.end());
139 return result;
140}
141
142static std::string get_ce_key_current_path(const std::string& directory_path) {
143 return directory_path + "/current";
144}
145
146static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700147 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700148 if (paths.empty()) {
149 *ce_key_path = get_ce_key_current_path(directory_path);
150 return true;
151 }
152 for (unsigned int i = 0; i < UINT_MAX; i++) {
153 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
154 if (paths[0] < candidate) {
155 *ce_key_path = candidate;
156 return true;
157 }
158 }
159 return false;
160}
161
162// Discard all keys but the named one; rename it to canonical name.
163// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700164static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700165 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700166 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700167 if (other_path != to_fix) {
168 android::vold::destroyKey(other_path);
169 }
170 }
171 auto const current_path = get_ce_key_current_path(directory_path);
172 if (to_fix != current_path) {
173 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
174 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
175 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800176 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700177 }
178 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800179 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700180}
181
182static bool read_and_fixate_user_ce_key(userid_t user_id,
183 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700184 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700185 auto const directory_path = get_ce_key_directory_path(user_id);
186 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700187 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700188 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
Paul Crowley4eac2642020-02-12 11:04:05 -0800189 if (retrieveKey(ce_key_path, auth, ce_key)) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 LOG(DEBUG) << "Successfully retrieved key";
191 fixate_user_ce_key(directory_path, ce_key_path, paths);
192 return true;
193 }
194 }
195 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
196 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100197}
198
Eric Biggers83a73d72019-09-30 13:06:47 -0700199// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley77df7f22020-01-23 15:29:30 -0800200static bool get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700201 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
202 if (entry == nullptr) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800203 LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
204 return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700205 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700206 if (!ParseOptions(entry->encryption_options, options)) {
207 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
208 << entry->encryption_options;
Paul Crowley77df7f22020-01-23 15:29:30 -0800209 return false;
Paul Crowleya50f6c32019-10-24 23:21:44 -0700210 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800211 return true;
Eric Biggers83a73d72019-09-30 13:06:47 -0700212}
213
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800214static bool install_storage_key(const std::string& mountpoint, const EncryptionOptions& options,
215 const KeyBuffer& key, EncryptionPolicy* policy) {
216 KeyBuffer ephemeral_wrapped_key;
217 if (options.use_hw_wrapped_key) {
218 if (!exportWrappedStorageKey(key, &ephemeral_wrapped_key)) {
219 LOG(ERROR) << "Failed to get ephemeral wrapped key";
220 return false;
221 }
222 }
223 return installKey(mountpoint, options, options.use_hw_wrapped_key ? ephemeral_wrapped_key : key,
224 policy);
225}
226
Eric Biggers83a73d72019-09-30 13:06:47 -0700227// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700228static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700229 auto contents_mode =
230 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
231 auto filenames_mode =
232 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800233 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
234 contents_mode + ":" + filenames_mode + ":v1");
235 if (!ParseOptions(options_string, options)) {
236 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
237 return false;
238 }
239 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700240}
241
Paul Crowleydf528a72016-03-09 09:31:37 -0800242static bool read_and_install_user_ce_key(userid_t user_id,
243 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800244 if (s_ce_policies.count(user_id) != 0) return true;
245 EncryptionOptions options;
246 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100247 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700248 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800249 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800250 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800251 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000252 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000253 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000254}
255
Paul Crowleydf528a72016-03-09 09:31:37 -0800256static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000257 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000258 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
259 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000260 return false;
261 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000262 return true;
263}
264
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600265static bool destroy_dir(const std::string& dir) {
266 LOG(DEBUG) << "Destroying: " << dir;
267 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
268 PLOG(ERROR) << "Failed to destroy " << dir;
269 return false;
270 }
271 return true;
272}
273
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000274// NB this assumes that there is only one thread listening for crypt commands, because
275// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000276static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800277 EncryptionOptions options;
278 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100279 KeyBuffer de_key, ce_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800280 if (!generateStorageKey(makeGen(options), &de_key)) return false;
281 if (!generateStorageKey(makeGen(options), &ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000282 if (create_ephemeral) {
283 // If the key should be created as ephemeral, don't store it.
284 s_ephemeral_users.insert(user_id);
285 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700286 auto const directory_path = get_ce_key_directory_path(user_id);
287 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
288 auto const paths = get_ce_key_paths(directory_path);
289 std::string ce_key_path;
290 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700291 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
292 ce_key))
293 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700294 fixate_user_ce_key(directory_path, ce_key_path, paths);
295 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700296 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700297 kEmptyAuthentication, de_key))
298 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100299 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800300 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800301 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800302 s_de_policies[user_id] = de_policy;
303 EncryptionPolicy ce_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800304 if (!install_storage_key(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800305 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000306 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000307 return true;
308}
309
Paul Crowley77df7f22020-01-23 15:29:30 -0800310static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
311 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000312 auto refi = key_map.find(user_id);
313 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700314 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000315 return false;
316 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800317 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000318 return true;
319}
320
Paul Crowleydf528a72016-03-09 09:31:37 -0800321static bool is_numeric(const char* name) {
322 for (const char* p = name; *p != '\0'; p++) {
323 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000324 }
325 return true;
326}
327
328static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800329 EncryptionOptions options;
330 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000331 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800332 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000333 if (!dirp) {
334 PLOG(ERROR) << "Unable to read de key directory";
335 return false;
336 }
337 for (;;) {
338 errno = 0;
339 auto entry = readdir(dirp.get());
340 if (!entry) {
341 if (errno) {
342 PLOG(ERROR) << "Unable to read de key directory";
343 return false;
344 }
345 break;
346 }
347 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
348 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
349 continue;
350 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600351 userid_t user_id = std::stoi(entry->d_name);
Paul Crowley77df7f22020-01-23 15:29:30 -0800352 if (s_de_policies.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000353 auto key_path = de_dir + "/" + entry->d_name;
Paul Crowley77df7f22020-01-23 15:29:30 -0800354 KeyBuffer de_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800355 if (!retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800356 EncryptionPolicy de_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800357 if (!install_storage_key(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800358 s_de_policies[user_id] = de_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000359 LOG(DEBUG) << "Installed de key for user " << user_id;
360 }
361 }
Eric Biggersa701c452018-10-23 13:06:55 -0700362 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000363 // correct policy set on them, and that no rogue ones exist.
364 return true;
365}
366
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700367bool fscrypt_initialize_systemwide_keys() {
368 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800369
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700370 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000371 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000372 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800373 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800374 EncryptionOptions options;
375 if (!get_data_file_encryption_options(&options)) return false;
376
377 KeyBuffer device_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800378 if (!retrieveOrGenerateKey(device_key_path, device_key_temp, kEmptyAuthentication,
379 makeGen(options), &device_key))
Paul Crowley77df7f22020-01-23 15:29:30 -0800380 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800381
Paul Crowley5e53ff62019-10-24 14:55:17 -0700382 EncryptionPolicy device_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800383 if (!install_storage_key(DATA_MNT_POINT, options, device_key, &device_policy)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700384
Paul Crowley5e53ff62019-10-24 14:55:17 -0700385 std::string options_string;
386 if (!OptionsToString(device_policy.options, &options_string)) {
387 LOG(ERROR) << "Unable to serialize options";
388 return false;
389 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800390 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700391 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700392
Paul Crowley77df7f22020-01-23 15:29:30 -0800393 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700394 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700395 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800396
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700397 KeyBuffer per_boot_key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800398 if (!generateStorageKey(makeGen(options), &per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800399 EncryptionPolicy per_boot_policy;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800400 if (!install_storage_key(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700401 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800402 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
403 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700404 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
405
Tommy Chiua98464f2019-03-26 14:14:19 +0800406 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700407 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000408 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800409}
410
Eric Biggersa701c452018-10-23 13:06:55 -0700411bool fscrypt_init_user0() {
412 LOG(DEBUG) << "fscrypt_init_user0";
413 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000414 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
415 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
416 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700417 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000418 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000419 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700420 // TODO: switch to loading only DE_0 here once framework makes
421 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000422 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000423 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700424 // We can only safely prepare DE storage here, since CE keys are probably
425 // entangled with user credentials. The framework will always prepare CE
426 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700427 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700428 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000429 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700430 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700431
432 // If this is a non-FBE device that recently left an emulated mode,
433 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700434 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
435 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700436 }
437
Paul Crowley76107cb2016-02-09 10:04:39 +0000438 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000439}
440
Eric Biggersa701c452018-10-23 13:06:55 -0700441bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
442 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
443 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000444 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000445 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000446 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800447 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700448 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800449 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000450 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000451 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800452 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000453 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000454 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000455 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000456 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800457}
458
Eric Biggersce368682019-04-03 15:44:06 -0700459// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700460// in the case where the keys are being put in the session keyring (rather in
461// the newer filesystem-level keyrings), because removing a key from the session
462// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
463// was already set up. So to remove the per-file keys and make the files
464// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700465//
466// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
467// objects systemwide. This is overkill, but it's the best available method
468// currently. Don't use drop_caches mode "3" because that also evicts pagecache
469// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700470static void drop_caches_if_needed() {
471 if (android::vold::isFsKeyringSupported()) {
472 return;
473 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100474 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700475 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100476 PLOG(ERROR) << "Failed to drop caches during key eviction";
477 }
478}
479
Andrew Scull7ec25c72016-10-31 10:28:25 +0000480static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000481 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800482 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000483 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800484 if (lookup_policy(s_ce_policies, user_id, &policy)) {
485 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700486 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000487 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800488 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000489 return success;
490}
491
Eric Biggersa701c452018-10-23 13:06:55 -0700492bool fscrypt_destroy_user_key(userid_t user_id) {
493 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
494 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000495 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000496 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000497 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000498 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800499 EncryptionPolicy de_policy;
500 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
501 android::vold::evictKey(DATA_MNT_POINT, de_policy);
502 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000503 auto it = s_ephemeral_users.find(user_id);
504 if (it != s_ephemeral_users.end()) {
505 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000506 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700507 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700508 success &= android::vold::destroyKey(path);
509 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700510 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700511 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700512 success &= android::vold::destroyKey(de_key_path);
513 } else {
514 LOG(INFO) << "Not present so not erasing: " << de_key_path;
515 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100516 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000517 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100518}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800519
Paul Crowley76107cb2016-02-09 10:04:39 +0000520static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700521 if (chmod(path.c_str(), 0000) != 0) {
522 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000523 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700524 }
525#if EMULATED_USES_SELINUX
526 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
527 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000528 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700529 }
530#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000531 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700532}
533
Paul Crowley76107cb2016-02-09 10:04:39 +0000534static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700535 if (chmod(path.c_str(), mode) != 0) {
536 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000537 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700538 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700539 }
540#if EMULATED_USES_SELINUX
541 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
542 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000543 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700544 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700545 }
546#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000547 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700548}
549
Paul Crowley3b71fc52017-10-09 10:55:21 -0700550static bool parse_hex(const std::string& hex, std::string* result) {
551 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800552 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000553 return true;
554 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800555 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800556 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000557 return false;
558 }
559 return true;
560}
561
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700562static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
563 const std::string& token_hex, const std::string& secret_hex) {
564 std::string token, secret;
565 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
566 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
567 if (secret.empty()) {
568 return kEmptyAuthentication;
569 } else {
570 return android::vold::KeyAuthentication(token, secret);
571 }
572}
573
Paul Crowley3aa914d2017-10-09 16:35:51 -0700574static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
575 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
576}
577
Paul Crowley26a53882017-10-26 11:16:39 -0700578static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
579 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
580}
581
Paul Crowley3aa914d2017-10-09 16:35:51 -0700582static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700583 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700584 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
585 std::string secdiscardable_hash;
586 if (android::vold::pathExists(secdiscardable_path)) {
587 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
588 return false;
589 } else {
590 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
591 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
592 return false;
593 }
594 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
595 return false;
596 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700597 auto key_path = volkey_path(misc_path, volume_uuid);
598 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
599 PLOG(ERROR) << "Creating directories for: " << key_path;
600 return false;
601 }
Paul Crowley26a53882017-10-26 11:16:39 -0700602 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700603
Paul Crowley77df7f22020-01-23 15:29:30 -0800604 EncryptionOptions options;
605 if (!get_volume_file_encryption_options(&options)) return false;
606 KeyBuffer key;
Paul Crowley4eac2642020-02-12 11:04:05 -0800607 if (!retrieveOrGenerateKey(key_path, key_path + "_tmp", auth, makeGen(options), &key))
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800608 return false;
609 if (!install_storage_key(BuildDataPath(volume_uuid), options, key, policy)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800610 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700611}
612
613static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700614 auto path = volkey_path(misc_path, volume_uuid);
615 if (!android::vold::pathExists(path)) return true;
616 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700617}
618
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700619static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
620 const android::vold::KeyAuthentication& retrieve_auth,
621 const android::vold::KeyAuthentication& store_auth) {
622 if (s_ephemeral_users.count(user_id) != 0) return true;
623 auto const directory_path = get_ce_key_directory_path(user_id);
624 KeyBuffer ce_key;
625 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
Paul Crowley4eac2642020-02-12 11:04:05 -0800626 if (retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700627 LOG(DEBUG) << "Successfully retrieved key";
628 // TODO(147732812): Remove this once Locksettingservice is fixed.
629 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
630 // changed from swipe to none or vice-versa
Paul Crowley4eac2642020-02-12 11:04:05 -0800631 } else if (retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700632 LOG(DEBUG) << "Successfully retrieved key with empty auth";
633 } else {
634 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
635 return false;
636 }
637 auto const paths = get_ce_key_paths(directory_path);
638 std::string ce_key_path;
639 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
640 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
641 return false;
642 if (!android::vold::FsyncDirectory(directory_path)) return false;
643 return true;
644}
645
Eric Biggersa701c452018-10-23 13:06:55 -0700646bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700647 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700648 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700649 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700650 if (!fscrypt_is_native()) return true;
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700651 auto auth = authentication_from_hex(token_hex, secret_hex);
652 if (!auth) return false;
653 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
654}
655
656bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
657 const std::string& secret_hex) {
658 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
659 << " token_present=" << (token_hex != "!");
660 if (!fscrypt_is_native()) return true;
661 auto auth = authentication_from_hex(token_hex, secret_hex);
662 if (!auth) return false;
663 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Paul Crowleya3630362016-05-17 14:17:56 -0700664}
665
Eric Biggersa701c452018-10-23 13:06:55 -0700666bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
667 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
668 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700669 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700670 auto const directory_path = get_ce_key_directory_path(user_id);
671 auto const paths = get_ce_key_paths(directory_path);
672 if (paths.empty()) {
673 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
674 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000675 }
Paul Crowleya3630362016-05-17 14:17:56 -0700676 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000677 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000678}
679
Jeff Sharkey47695b22016-02-01 17:02:29 -0700680// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700681bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700682 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700683 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700684 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700685 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800686 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000687 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000688 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000689 }
Barani Muthukumaranb1927c22019-10-31 22:59:34 -0700690 auto auth = authentication_from_hex(token_hex, secret_hex);
691 if (!auth) return false;
692 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000693 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000694 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800695 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700696 } else {
697 // When in emulation mode, we just use chmod. However, we also
698 // unlock directories when not in emulation mode, to bring devices
699 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000700 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800701 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700702 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
703 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700704 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000705 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700706 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800707 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000708 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800709}
710
Jeff Sharkey47695b22016-02-01 17:02:29 -0700711// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700712bool fscrypt_lock_user_key(userid_t user_id) {
713 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
714 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000715 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700716 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800717 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000718 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800719 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700720 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
721 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000722 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000723 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800724 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800725 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700726
Paul Crowley76107cb2016-02-09 10:04:39 +0000727 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800728}
729
Paul Crowley82b41ff2017-10-20 08:17:54 -0700730static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
731 userid_t user_id, int flags) {
732 if (0 != android::vold::ForkExecvp(
733 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
734 std::to_string(user_id), std::to_string(flags)})) {
735 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700736 return false;
737 }
738 return true;
739}
740
Eric Biggersa701c452018-10-23 13:06:55 -0700741bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700742 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700743 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800744 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700745
Paul Crowley82b41ff2017-10-20 08:17:54 -0700746 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600747 // DE_sys key
748 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
749 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
750 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600751
752 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700753 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
754 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800755 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700756 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
757
Paul Crowley3b71fc52017-10-09 10:55:21 -0700758 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700759 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600760#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700761 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700762 multiuser_get_uid(user_id, AID_EVERYBODY)))
763 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600764#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700765 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600766
Paul Crowley6b756ce2017-10-05 14:07:09 -0700767 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
768 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800769 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700770 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000771 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000772
Eric Biggersa701c452018-10-23 13:06:55 -0700773 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700774 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700775 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800776 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700777 if (!EnsurePolicy(de_policy, system_de_path)) return false;
778 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
779 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700780 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700781 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700782 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700783 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700784 }
Paul Crowley285956f2016-01-20 13:12:38 +0000785 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800786
Paul Crowley82b41ff2017-10-20 08:17:54 -0700787 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600788 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700789 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
790 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800791 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600792 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
793 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800794
Paul Crowley3b71fc52017-10-09 10:55:21 -0700795 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700796 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
797 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800798 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700799 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000800 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
801 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700802
Eric Biggersa701c452018-10-23 13:06:55 -0700803 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700804 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700805 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800806 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700807 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
808 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
809 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700810 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700811 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700812 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700813 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
814 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700815 }
Paul Crowley1a965262017-10-13 13:49:50 -0700816
817 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700818 // Now that credentials have been installed, we can run restorecon
819 // over these paths
820 // NOTE: these paths need to be kept in sync with libselinux
821 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700822 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700823 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700824 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800825 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700826 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800827
Paul Crowley76107cb2016-02-09 10:04:39 +0000828 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800829}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600830
Eric Biggersa701c452018-10-23 13:06:55 -0700831bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
832 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600833 << ", user " << user_id << ", flags " << flags;
834 bool res = true;
835
Paul Crowley82b41ff2017-10-20 08:17:54 -0700836 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
837
838 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700839 // CE_n key
840 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
841 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800842 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700843 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
844 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
845
846 res &= destroy_dir(media_ce_path);
847 res &= destroy_dir(user_ce_path);
848 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700849 res &= destroy_dir(system_ce_path);
850 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800851 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700852 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700853 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700854 res &= destroy_volkey(misc_ce_path, volume_uuid);
855 }
Paul Crowley8e550662017-10-16 17:01:44 -0700856 }
857 }
858
Paul Crowley82b41ff2017-10-20 08:17:54 -0700859 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600860 // DE_sys key
861 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
862 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
863 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600864
865 // DE_n key
866 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
867 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800868 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600869 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
870
Paul Crowley8e550662017-10-16 17:01:44 -0700871 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700872 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600873 res &= destroy_dir(system_legacy_path);
874#if MANAGE_MISC_DIRS
875 res &= destroy_dir(misc_legacy_path);
876#endif
877 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600878 res &= destroy_dir(system_de_path);
879 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800880 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700881 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700882 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700883 res &= destroy_volkey(misc_de_path, volume_uuid);
884 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600885 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600886 }
887
888 return res;
889}
Rubin Xu2436e272017-04-27 20:43:10 +0100890
Paul Crowleyc6433a22017-10-24 14:54:43 -0700891static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
892 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
893 if (!dirp) {
894 PLOG(ERROR) << "Unable to open directory: " + directory_path;
895 return false;
896 }
897 bool res = true;
898 for (;;) {
899 errno = 0;
900 auto const entry = readdir(dirp.get());
901 if (!entry) {
902 if (errno) {
903 PLOG(ERROR) << "Unable to read directory: " + directory_path;
904 return false;
905 }
906 break;
907 }
908 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
909 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
910 continue;
911 }
912 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
913 }
914 return res;
915}
916
Eric Biggersa701c452018-10-23 13:06:55 -0700917bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700918 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700919 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700920 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
921 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700922 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
923 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
924 return res;
925}