blob: 5b01d5d00570d3239cb144ba4a5cdcb53dc945c2 [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Shivaprasad Hongal92292622018-07-05 14:49:12 -070019#include "Keymaster.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000020#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080022#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070023#include "VoldUtil.h"
24
Paul Crowleya3630362016-05-17 14:17:56 -070025#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000026#include <map>
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -070027#include <optional>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000028#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070029#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080030#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000032
Paul Crowleydf528a72016-03-09 09:31:37 -080033#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070036#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070037#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080038#include <sys/mount.h>
39#include <sys/stat.h>
40#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000042
Paul Crowley480fcd22015-08-24 14:53:28 +010043#include <private/android_filesystem_config.h>
44
Paul Crowley82b41ff2017-10-20 08:17:54 -070045#include "android/os/IVold.h"
46
Paul Lawrence731a7a22015-04-28 22:14:15 +000047#include "cryptfs.h"
48
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060050#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070051
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080052#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070053#include <cutils/properties.h>
54
Eric Biggersa701c452018-10-23 13:06:55 -070055#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070056#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080057
Elliott Hughes7e128fb2015-12-04 15:50:53 -080058#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080059#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070060#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080061#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070062#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080063#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000064
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080065using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080066using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley77df7f22020-01-23 15:29:30 -080067using android::vold::BuildDataPath;
Paul Crowley05720802016-02-08 15:55:41 +000068using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010069using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070070using android::vold::Keymaster;
71using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080072using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070073using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080074
Paul Lawrence731a7a22015-04-28 22:14:15 +000075namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000076
Eric Biggersa701c452018-10-23 13:06:55 -070077const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080078const std::string device_key_path = device_key_dir + "/key";
79const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080080
Paul Crowleydf528a72016-03-09 09:31:37 -080081const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
82const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070083const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000084
Paul Crowley26a53882017-10-26 11:16:39 -070085const std::string systemwide_volume_key_dir =
86 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
87
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070088bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080089
Paul Crowleydf528a72016-03-09 09:31:37 -080090// Some users are ephemeral, don't try to wipe their keys from disk
91std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080092
Paul Crowley77df7f22020-01-23 15:29:30 -080093// Map user ids to encryption policies
94std::map<userid_t, EncryptionPolicy> s_de_policies;
95std::map<userid_t, EncryptionPolicy> s_ce_policies;
Paul Lawrence731a7a22015-04-28 22:14:15 +000096
Paul Crowley14c8c072018-09-18 13:30:21 -070097} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000098
Eric Biggersa701c452018-10-23 13:06:55 -070099static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000100 return property_get_bool("persist.sys.emulate_fbe", false);
101}
102
Paul Crowley3b71fc52017-10-09 10:55:21 -0700103static const char* escape_empty(const std::string& value) {
104 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000105}
106
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000107static std::string get_de_key_path(userid_t user_id) {
108 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
109}
110
Paul Crowleya3630362016-05-17 14:17:56 -0700111static std::string get_ce_key_directory_path(userid_t user_id) {
112 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
113}
114
115// Returns the keys newest first
116static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
117 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
118 if (!dirp) {
119 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
120 return std::vector<std::string>();
121 }
122 std::vector<std::string> result;
123 for (;;) {
124 errno = 0;
125 auto const entry = readdir(dirp.get());
126 if (!entry) {
127 if (errno) {
128 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
129 return std::vector<std::string>();
130 }
131 break;
132 }
133 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
134 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
135 continue;
136 }
137 result.emplace_back(directory_path + "/" + entry->d_name);
138 }
139 std::sort(result.begin(), result.end());
140 std::reverse(result.begin(), result.end());
141 return result;
142}
143
144static std::string get_ce_key_current_path(const std::string& directory_path) {
145 return directory_path + "/current";
146}
147
148static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700149 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700150 if (paths.empty()) {
151 *ce_key_path = get_ce_key_current_path(directory_path);
152 return true;
153 }
154 for (unsigned int i = 0; i < UINT_MAX; i++) {
155 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
156 if (paths[0] < candidate) {
157 *ce_key_path = candidate;
158 return true;
159 }
160 }
161 return false;
162}
163
164// Discard all keys but the named one; rename it to canonical name.
165// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700166static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700167 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700169 if (other_path != to_fix) {
170 android::vold::destroyKey(other_path);
171 }
172 }
173 auto const current_path = get_ce_key_current_path(directory_path);
174 if (to_fix != current_path) {
175 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
176 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
177 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800178 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700179 }
180 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800181 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700182}
183
184static bool read_and_fixate_user_ce_key(userid_t user_id,
185 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700187 auto const directory_path = get_ce_key_directory_path(user_id);
188 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
191 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
192 LOG(DEBUG) << "Successfully retrieved key";
193 fixate_user_ce_key(directory_path, ce_key_path, paths);
194 return true;
195 }
196 }
197 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
198 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100199}
200
Eric Biggers83a73d72019-09-30 13:06:47 -0700201// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley77df7f22020-01-23 15:29:30 -0800202static bool get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700203 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
204 if (entry == nullptr) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800205 LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
206 return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700207 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700208 if (!ParseOptions(entry->encryption_options, options)) {
209 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
210 << entry->encryption_options;
Paul Crowley77df7f22020-01-23 15:29:30 -0800211 return false;
Paul Crowleya50f6c32019-10-24 23:21:44 -0700212 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800213 return true;
Eric Biggers83a73d72019-09-30 13:06:47 -0700214}
215
216// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700217static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700218 auto contents_mode =
Barani Muthukumaranff525592020-01-29 14:38:31 -0800219 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700220 auto filenames_mode =
Eric Biggers83a73d72019-09-30 13:06:47 -0700221 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowley77df7f22020-01-23 15:29:30 -0800222 auto options_string = android::base::GetProperty("ro.crypto.volume.options",
223 contents_mode + ":" + filenames_mode + ":v1");
224 if (!ParseOptions(options_string, options)) {
225 LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
226 return false;
227 }
228 return true;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700229}
230
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700231bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800232 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700233}
234
Neeraj Sonic480f912018-12-14 15:18:15 +0530235bool is_metadata_wrapped_key_supported() {
236 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
237}
238
Paul Crowleydf528a72016-03-09 09:31:37 -0800239static bool read_and_install_user_ce_key(userid_t user_id,
240 const android::vold::KeyAuthentication& auth) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800241 if (s_ce_policies.count(user_id) != 0) return true;
242 EncryptionOptions options;
243 if (!get_data_file_encryption_options(&options)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100244 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700245 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700246 if (is_wrapped_key_supported()) {
247 KeyBuffer ephemeral_wrapped_key;
248 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
249 LOG(ERROR) << "Failed to export ce key";
250 return false;
251 }
252 ce_key = std::move(ephemeral_wrapped_key);
253 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800254 EncryptionPolicy ce_policy;
255 if (!installKey(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
256 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;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700285
286 if(is_wrapped_key_supported()) {
287 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
288 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
289 } else {
290 if (!android::vold::randomKey(&de_key)) return false;
291 if (!android::vold::randomKey(&ce_key)) return false;
292 }
293
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000294 if (create_ephemeral) {
295 // If the key should be created as ephemeral, don't store it.
296 s_ephemeral_users.insert(user_id);
297 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700298 auto const directory_path = get_ce_key_directory_path(user_id);
299 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
300 auto const paths = get_ce_key_paths(directory_path);
301 std::string ce_key_path;
302 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700303 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
304 ce_key))
305 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700306 fixate_user_ce_key(directory_path, ce_key_path, paths);
307 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700308 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700309 kEmptyAuthentication, de_key))
310 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100311 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700312 if (is_wrapped_key_supported()) {
313 KeyBuffer ephemeral_wrapped_de_key;
314 KeyBuffer ephemeral_wrapped_ce_key;
315
316 /* Export and install the DE keys */
317 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
318 LOG(ERROR) << "Failed to export de_key";
319 return false;
320 }
321 /* Export and install the CE keys */
322 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
323 LOG(ERROR) << "Failed to export de_key";
324 return false;
325 }
326
327 de_key = std::move(ephemeral_wrapped_de_key);
328 ce_key = std::move(ephemeral_wrapped_ce_key);
329 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800330 EncryptionPolicy de_policy;
331 if (!installKey(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
332 s_de_policies[user_id] = de_policy;
333 EncryptionPolicy ce_policy;
334 if (!installKey(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
335 s_ce_policies[user_id] = ce_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000336 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000337 return true;
338}
339
Paul Crowley77df7f22020-01-23 15:29:30 -0800340static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
341 EncryptionPolicy* policy) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000342 auto refi = key_map.find(user_id);
343 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700344 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000345 return false;
346 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800347 *policy = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000348 return true;
349}
350
Paul Crowleydf528a72016-03-09 09:31:37 -0800351static bool is_numeric(const char* name) {
352 for (const char* p = name; *p != '\0'; p++) {
353 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000354 }
355 return true;
356}
357
358static bool load_all_de_keys() {
Paul Crowley77df7f22020-01-23 15:29:30 -0800359 EncryptionOptions options;
360 if (!get_data_file_encryption_options(&options)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000361 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800362 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000363 if (!dirp) {
364 PLOG(ERROR) << "Unable to read de key directory";
365 return false;
366 }
367 for (;;) {
368 errno = 0;
369 auto entry = readdir(dirp.get());
370 if (!entry) {
371 if (errno) {
372 PLOG(ERROR) << "Unable to read de key directory";
373 return false;
374 }
375 break;
376 }
377 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
378 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
379 continue;
380 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600381 userid_t user_id = std::stoi(entry->d_name);
Paul Crowley77df7f22020-01-23 15:29:30 -0800382 if (s_de_policies.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000383 auto key_path = de_dir + "/" + entry->d_name;
Paul Crowley77df7f22020-01-23 15:29:30 -0800384 KeyBuffer de_key;
385 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700386 if (is_wrapped_key_supported()) {
387 KeyBuffer ephemeral_wrapped_key;
Steven Lavered7b3532020-02-12 12:49:40 -0800388 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_key)) {
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700389 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
390 return false;
391 }
Steven Lavered7b3532020-02-12 12:49:40 -0800392 de_key = std::move(ephemeral_wrapped_key);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700393 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800394 EncryptionPolicy de_policy;
395 if (!installKey(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
396 s_de_policies[user_id] = de_policy;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000397 LOG(DEBUG) << "Installed de key for user " << user_id;
398 }
399 }
Eric Biggersa701c452018-10-23 13:06:55 -0700400 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000401 // correct policy set on them, and that no rogue ones exist.
402 return true;
403}
404
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700405bool fscrypt_initialize_systemwide_keys() {
406 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700407 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800408
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700409 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000410 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000411 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800412 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800413 EncryptionOptions options;
414 if (!get_data_file_encryption_options(&options)) return false;
415
416 KeyBuffer device_key;
417 if (!android::vold::retrieveKey(true, kEmptyAuthentication, device_key_path, device_key_temp,
Steven Lavered7b3532020-02-12 12:49:40 -0800418 is_wrapped_key_supported(), &device_key))
Paul Crowley77df7f22020-01-23 15:29:30 -0800419 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800420
Paul Crowley5e53ff62019-10-24 14:55:17 -0700421 EncryptionPolicy device_policy;
Paul Crowley77df7f22020-01-23 15:29:30 -0800422 if (!android::vold::installKey(DATA_MNT_POINT, options, device_key, &device_policy))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700423 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800424
Paul Crowley5e53ff62019-10-24 14:55:17 -0700425 std::string options_string;
426 if (!OptionsToString(device_policy.options, &options_string)) {
427 LOG(ERROR) << "Unable to serialize options";
428 return false;
429 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800430 std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -0700431 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700432
Paul Crowley77df7f22020-01-23 15:29:30 -0800433 std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700434 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700435 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800436
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700437 KeyBuffer per_boot_key;
438 if (!android::vold::randomKey(&per_boot_key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800439 EncryptionPolicy per_boot_policy;
440 if (!android::vold::installKey(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy))
441 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700442 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
Paul Crowley77df7f22020-01-23 15:29:30 -0800443 if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
444 return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700445 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
446
Tommy Chiua98464f2019-03-26 14:14:19 +0800447 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700448 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000449 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800450}
451
Eric Biggersa701c452018-10-23 13:06:55 -0700452bool fscrypt_init_user0() {
453 LOG(DEBUG) << "fscrypt_init_user0";
454 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000455 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
456 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
457 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700458 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000459 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000460 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700461 // TODO: switch to loading only DE_0 here once framework makes
462 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000463 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000464 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700465 // We can only safely prepare DE storage here, since CE keys are probably
466 // entangled with user credentials. The framework will always prepare CE
467 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700468 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700469 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000470 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700471 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700472
473 // If this is a non-FBE device that recently left an emulated mode,
474 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700475 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
476 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700477 }
478
Paul Crowley76107cb2016-02-09 10:04:39 +0000479 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000480}
481
Eric Biggersa701c452018-10-23 13:06:55 -0700482bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
483 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
484 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000485 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000486 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000487 // FIXME test for existence of key that is not loaded yet
Paul Crowley77df7f22020-01-23 15:29:30 -0800488 if (s_ce_policies.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700489 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800490 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000491 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000492 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800493 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000494 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000495 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000496 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000497 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800498}
499
Eric Biggersce368682019-04-03 15:44:06 -0700500// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700501// in the case where the keys are being put in the session keyring (rather in
502// the newer filesystem-level keyrings), because removing a key from the session
503// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
504// was already set up. So to remove the per-file keys and make the files
505// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700506//
507// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
508// objects systemwide. This is overkill, but it's the best available method
509// currently. Don't use drop_caches mode "3" because that also evicts pagecache
510// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700511static void drop_caches_if_needed() {
512 if (android::vold::isFsKeyringSupported()) {
513 return;
514 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100515 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700516 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100517 PLOG(ERROR) << "Failed to drop caches during key eviction";
518 }
519}
520
Andrew Scull7ec25c72016-10-31 10:28:25 +0000521static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000522 bool success = true;
Paul Crowley77df7f22020-01-23 15:29:30 -0800523 EncryptionPolicy policy;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000524 // If we haven't loaded the CE key, no need to evict it.
Paul Crowley77df7f22020-01-23 15:29:30 -0800525 if (lookup_policy(s_ce_policies, user_id, &policy)) {
526 success &= android::vold::evictKey(DATA_MNT_POINT, policy);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700527 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000528 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800529 s_ce_policies.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000530 return success;
531}
532
Eric Biggersa701c452018-10-23 13:06:55 -0700533bool fscrypt_destroy_user_key(userid_t user_id) {
534 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
535 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000536 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000537 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000538 bool success = true;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000539 success &= evict_ce_key(user_id);
Paul Crowley77df7f22020-01-23 15:29:30 -0800540 EncryptionPolicy de_policy;
541 success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
542 android::vold::evictKey(DATA_MNT_POINT, de_policy);
543 s_de_policies.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000544 auto it = s_ephemeral_users.find(user_id);
545 if (it != s_ephemeral_users.end()) {
546 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000547 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700548 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700549 success &= android::vold::destroyKey(path);
550 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700551 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700552 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700553 success &= android::vold::destroyKey(de_key_path);
554 } else {
555 LOG(INFO) << "Not present so not erasing: " << de_key_path;
556 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100557 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000558 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100559}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800560
Paul Crowley76107cb2016-02-09 10:04:39 +0000561static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700562 if (chmod(path.c_str(), 0000) != 0) {
563 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000564 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700565 }
566#if EMULATED_USES_SELINUX
567 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
568 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000569 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700570 }
571#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000572 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700573}
574
Paul Crowley76107cb2016-02-09 10:04:39 +0000575static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700576 if (chmod(path.c_str(), mode) != 0) {
577 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000578 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700579 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700580 }
581#if EMULATED_USES_SELINUX
582 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
583 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000584 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700585 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700586 }
587#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000588 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700589}
590
Paul Crowley3b71fc52017-10-09 10:55:21 -0700591static bool parse_hex(const std::string& hex, std::string* result) {
592 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800593 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000594 return true;
595 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800596 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800597 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000598 return false;
599 }
600 return true;
601}
602
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700603static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
604 const std::string& token_hex, const std::string& secret_hex) {
605 std::string token, secret;
606 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
607 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
608 if (secret.empty()) {
609 return kEmptyAuthentication;
610 } else {
611 return android::vold::KeyAuthentication(token, secret);
612 }
613}
614
Paul Crowley3aa914d2017-10-09 16:35:51 -0700615static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
616 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
617}
618
Paul Crowley26a53882017-10-26 11:16:39 -0700619static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
620 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
621}
622
Paul Crowley3aa914d2017-10-09 16:35:51 -0700623static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800624 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700625 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
626 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700627 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700628 if (android::vold::pathExists(secdiscardable_path)) {
629 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
630 return false;
631 } else {
632 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
633 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
634 return false;
635 }
636 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
637 return false;
638 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700639 auto key_path = volkey_path(misc_path, volume_uuid);
640 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
641 PLOG(ERROR) << "Creating directories for: " << key_path;
642 return false;
643 }
Paul Crowley26a53882017-10-26 11:16:39 -0700644 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700645
Paul Crowley77df7f22020-01-23 15:29:30 -0800646 EncryptionOptions options;
647 if (!get_volume_file_encryption_options(&options)) return false;
648 KeyBuffer key;
Steven Lavered7b3532020-02-12 12:49:40 -0800649 if (!android::vold::retrieveKey(true, auth, key_path, key_path + "_tmp", false, &key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800650 if (!android::vold::installKey(BuildDataPath(volume_uuid), options, key, policy)) return false;
651 return true;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700652}
653
654static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700655 auto path = volkey_path(misc_path, volume_uuid);
656 if (!android::vold::pathExists(path)) return true;
657 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700658}
659
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700660static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
661 const android::vold::KeyAuthentication& retrieve_auth,
662 const android::vold::KeyAuthentication& store_auth) {
663 if (s_ephemeral_users.count(user_id) != 0) return true;
664 auto const directory_path = get_ce_key_directory_path(user_id);
665 KeyBuffer ce_key;
666 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
667 if (android::vold::retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
668 LOG(DEBUG) << "Successfully retrieved key";
669 // TODO(147732812): Remove this once Locksettingservice is fixed.
670 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
671 // changed from swipe to none or vice-versa
672 } else if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
673 LOG(DEBUG) << "Successfully retrieved key with empty auth";
674 } else {
675 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
676 return false;
677 }
678 auto const paths = get_ce_key_paths(directory_path);
Steven Laver4771d982020-01-23 17:03:42 -0800679
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700680 std::string ce_key_path;
681 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
682 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
683 return false;
684 if (!android::vold::FsyncDirectory(directory_path)) return false;
685 return true;
686}
687
Eric Biggersa701c452018-10-23 13:06:55 -0700688bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700689 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700690 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700691 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700692 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700693 auto auth = authentication_from_hex(token_hex, secret_hex);
694 if (!auth) return false;
695 return fscrypt_rewrap_user_key(user_id, serial, kEmptyAuthentication, *auth);
Paul Crowleya3630362016-05-17 14:17:56 -0700696}
697
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800698bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700699 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800700 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700701 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800702 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700703 auto auth = authentication_from_hex(token_hex, secret_hex);
704 if (!auth) return false;
705 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700706}
707
Eric Biggersa701c452018-10-23 13:06:55 -0700708bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
709 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
710 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700711 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700712 auto const directory_path = get_ce_key_directory_path(user_id);
713 auto const paths = get_ce_key_paths(directory_path);
714 if (paths.empty()) {
715 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
716 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000717 }
Paul Crowleya3630362016-05-17 14:17:56 -0700718 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000719 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000720}
721
Jeff Sharkey47695b22016-02-01 17:02:29 -0700722// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700723bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700724 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700725 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700726 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700727 if (fscrypt_is_native()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800728 if (s_ce_policies.count(user_id) != 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000729 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000730 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000731 }
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700732 auto auth = authentication_from_hex(token_hex, secret_hex);
733 if (!auth) return false;
734 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000735 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000736 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800737 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700738 } else {
739 // When in emulation mode, we just use chmod. However, we also
740 // unlock directories when not in emulation mode, to bring devices
741 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000742 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800743 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700744 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
745 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700746 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000747 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700748 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800749 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000750 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800751}
752
Jeff Sharkey47695b22016-02-01 17:02:29 -0700753// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700754bool fscrypt_lock_user_key(userid_t user_id) {
755 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
756 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000757 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700758 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800759 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000760 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800761 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700762 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
763 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000764 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000765 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800766 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800767 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700768
Paul Crowley76107cb2016-02-09 10:04:39 +0000769 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800770}
771
Paul Crowley82b41ff2017-10-20 08:17:54 -0700772static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
773 userid_t user_id, int flags) {
774 if (0 != android::vold::ForkExecvp(
775 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
776 std::to_string(user_id), std::to_string(flags)})) {
777 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700778 return false;
779 }
780 return true;
781}
782
Eric Biggersa701c452018-10-23 13:06:55 -0700783bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700784 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700785 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800786 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700787
Paul Crowley82b41ff2017-10-20 08:17:54 -0700788 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600789 // DE_sys key
790 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
791 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
792 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600793
794 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700795 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
796 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800797 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700798 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
799
Paul Crowley3b71fc52017-10-09 10:55:21 -0700800 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700801 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600802#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700803 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700804 multiuser_get_uid(user_id, AID_EVERYBODY)))
805 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600806#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700807 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600808
Paul Crowley6b756ce2017-10-05 14:07:09 -0700809 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
810 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800811 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700812 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000813 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000814
Eric Biggersa701c452018-10-23 13:06:55 -0700815 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700816 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700817 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800818 if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700819 if (!EnsurePolicy(de_policy, system_de_path)) return false;
820 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
821 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700822 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800823 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700824 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700825 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700826 }
Paul Crowley285956f2016-01-20 13:12:38 +0000827 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800828
Paul Crowley82b41ff2017-10-20 08:17:54 -0700829 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600830 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700831 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
832 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800833 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600834 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
835 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800836
Paul Crowley3b71fc52017-10-09 10:55:21 -0700837 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700838 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
839 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800840 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700841 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000842 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
843 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700844
Eric Biggersa701c452018-10-23 13:06:55 -0700845 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700846 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700847 if (volume_uuid.empty()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800848 if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700849 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
850 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
851 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700852 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800853 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700854 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700855 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
856 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700857 }
Paul Crowley1a965262017-10-13 13:49:50 -0700858
859 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700860 // Now that credentials have been installed, we can run restorecon
861 // over these paths
862 // NOTE: these paths need to be kept in sync with libselinux
863 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700864 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700865 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700866 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800867 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700868 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800869
Paul Crowley76107cb2016-02-09 10:04:39 +0000870 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800871}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600872
Eric Biggersa701c452018-10-23 13:06:55 -0700873bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
874 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600875 << ", user " << user_id << ", flags " << flags;
876 bool res = true;
877
Paul Crowley82b41ff2017-10-20 08:17:54 -0700878 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
879
880 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700881 // CE_n key
882 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
883 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800884 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700885 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
886 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
887
888 res &= destroy_dir(media_ce_path);
889 res &= destroy_dir(user_ce_path);
890 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700891 res &= destroy_dir(system_ce_path);
892 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800893 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700894 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700895 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700896 res &= destroy_volkey(misc_ce_path, volume_uuid);
897 }
Paul Crowley8e550662017-10-16 17:01:44 -0700898 }
899 }
900
Paul Crowley82b41ff2017-10-20 08:17:54 -0700901 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600902 // DE_sys key
903 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
904 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
905 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600906
907 // DE_n key
908 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
909 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800910 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600911 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
912
Paul Crowley8e550662017-10-16 17:01:44 -0700913 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700914 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600915 res &= destroy_dir(system_legacy_path);
916#if MANAGE_MISC_DIRS
917 res &= destroy_dir(misc_legacy_path);
918#endif
919 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600920 res &= destroy_dir(system_de_path);
921 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800922 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700923 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700924 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700925 res &= destroy_volkey(misc_de_path, volume_uuid);
926 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600927 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600928 }
929
930 return res;
931}
Rubin Xu2436e272017-04-27 20:43:10 +0100932
Paul Crowleyc6433a22017-10-24 14:54:43 -0700933static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
934 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
935 if (!dirp) {
936 PLOG(ERROR) << "Unable to open directory: " + directory_path;
937 return false;
938 }
939 bool res = true;
940 for (;;) {
941 errno = 0;
942 auto const entry = readdir(dirp.get());
943 if (!entry) {
944 if (errno) {
945 PLOG(ERROR) << "Unable to read directory: " + directory_path;
946 return false;
947 }
948 break;
949 }
950 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
951 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
952 continue;
953 }
954 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
955 }
956 return res;
957}
958
Eric Biggersa701c452018-10-23 13:06:55 -0700959bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700960 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700961 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700962 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
963 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700964 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
965 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
966 return res;
967}