blob: e5d2caad938a0670c23155f986aa9a20a414da41 [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Shivaprasad Hongal92292622018-07-05 14:49:12 -070019#include "Keymaster.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000020#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080022#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070023#include "VoldUtil.h"
Gaurav Kashyap38cbb942019-07-17 18:11:57 -070024#include "model/Disk.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070025
Paul Crowleya3630362016-05-17 14:17:56 -070026#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000027#include <map>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000028#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070029#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080030#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000032
Paul Crowleydf528a72016-03-09 09:31:37 -080033#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070036#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070037#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080038#include <sys/mount.h>
39#include <sys/stat.h>
40#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000042
Paul Crowley480fcd22015-08-24 14:53:28 +010043#include <private/android_filesystem_config.h>
44
Paul Crowley82b41ff2017-10-20 08:17:54 -070045#include "android/os/IVold.h"
46
Paul Lawrence731a7a22015-04-28 22:14:15 +000047#include "cryptfs.h"
48
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060050#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070051
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080052#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070053#include <cutils/properties.h>
54
Eric Biggersa701c452018-10-23 13:06:55 -070055#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070056#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080057
Elliott Hughes7e128fb2015-12-04 15:50:53 -080058#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080059#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070060#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080061#include <android-base/stringprintf.h>
Jieb6698d52018-11-12 15:26:02 +080062#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000063
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080064using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080065using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000066using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010067using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070068using android::vold::Keymaster;
69using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080070using android::vold::writeStringToFile;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080071
Paul Lawrence731a7a22015-04-28 22:14:15 +000072namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000073
Paul Crowley3aa914d2017-10-09 16:35:51 -070074struct PolicyKeyRef {
75 std::string contents_mode;
76 std::string filenames_mode;
77 std::string key_raw_ref;
78};
79
Eric Biggersa701c452018-10-23 13:06:55 -070080const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080081const std::string device_key_path = device_key_dir + "/key";
82const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080083
Paul Crowleydf528a72016-03-09 09:31:37 -080084const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
85const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070086const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000087
Paul Crowley26a53882017-10-26 11:16:39 -070088const std::string systemwide_volume_key_dir =
89 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
90
Paul Crowleydf528a72016-03-09 09:31:37 -080091bool s_global_de_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080092
Paul Crowleydf528a72016-03-09 09:31:37 -080093// Some users are ephemeral, don't try to wipe their keys from disk
94std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080095
Paul Crowleydf528a72016-03-09 09:31:37 -080096// Map user ids to key references
97std::map<userid_t, std::string> s_de_key_raw_refs;
98std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070099// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +0100100std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +0000101
Paul Crowley14c8c072018-09-18 13:30:21 -0700102} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +0000103
Eric Biggersa701c452018-10-23 13:06:55 -0700104static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000105 return property_get_bool("persist.sys.emulate_fbe", false);
106}
107
Paul Crowley3b71fc52017-10-09 10:55:21 -0700108static const char* escape_empty(const std::string& value) {
109 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000110}
111
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000112static std::string get_de_key_path(userid_t user_id) {
113 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
114}
115
Paul Crowleya3630362016-05-17 14:17:56 -0700116static std::string get_ce_key_directory_path(userid_t user_id) {
117 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
118}
119
120// Returns the keys newest first
121static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
122 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
123 if (!dirp) {
124 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
125 return std::vector<std::string>();
126 }
127 std::vector<std::string> result;
128 for (;;) {
129 errno = 0;
130 auto const entry = readdir(dirp.get());
131 if (!entry) {
132 if (errno) {
133 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
134 return std::vector<std::string>();
135 }
136 break;
137 }
138 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
139 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
140 continue;
141 }
142 result.emplace_back(directory_path + "/" + entry->d_name);
143 }
144 std::sort(result.begin(), result.end());
145 std::reverse(result.begin(), result.end());
146 return result;
147}
148
149static std::string get_ce_key_current_path(const std::string& directory_path) {
150 return directory_path + "/current";
151}
152
153static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700154 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700155 if (paths.empty()) {
156 *ce_key_path = get_ce_key_current_path(directory_path);
157 return true;
158 }
159 for (unsigned int i = 0; i < UINT_MAX; i++) {
160 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
161 if (paths[0] < candidate) {
162 *ce_key_path = candidate;
163 return true;
164 }
165 }
166 return false;
167}
168
169// Discard all keys but the named one; rename it to canonical name.
170// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700171static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700172 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700173 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700174 if (other_path != to_fix) {
175 android::vold::destroyKey(other_path);
176 }
177 }
178 auto const current_path = get_ce_key_current_path(directory_path);
179 if (to_fix != current_path) {
180 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
181 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
182 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800183 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700184 }
185 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800186 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700187}
188
189static bool read_and_fixate_user_ce_key(userid_t user_id,
190 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700191 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700192 auto const directory_path = get_ce_key_directory_path(user_id);
193 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700194 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700195 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
196 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
197 LOG(DEBUG) << "Successfully retrieved key";
198 fixate_user_ce_key(directory_path, ce_key_path, paths);
199 return true;
200 }
201 }
202 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
203 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100204}
205
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700206bool is_ice_supported_external(int flags) {
207 /*
208 * Logic can be changed when more card controllers start supporting ICE.
209 * Until then, checking only for UFS card.
210 */
211 if ((flags & android::vold::Disk::Flags::kUfsCard) ==
212 android::vold::Disk::Flags::kUfsCard)
213 return true;
214 return false;
215}
216
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700217bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800218 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700219}
220
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700221bool is_wrapped_key_supported_external(int flags) {
222 if (is_ice_supported_external(flags))
223 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700224 return false;
225}
226
Neeraj Sonic480f912018-12-14 15:18:15 +0530227bool is_metadata_wrapped_key_supported() {
228 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
229}
230
Paul Crowleydf528a72016-03-09 09:31:37 -0800231static bool read_and_install_user_ce_key(userid_t user_id,
232 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000233 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100234 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700235 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000236 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700237
238 if (is_wrapped_key_supported()) {
239 KeyBuffer ephemeral_wrapped_key;
240 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
241 LOG(ERROR) << "Failed to export ce key";
242 return false;
243 }
244 ce_key = std::move(ephemeral_wrapped_key);
245 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700246 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100247 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000248 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000249 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000250 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000251}
252
Paul Crowleydf528a72016-03-09 09:31:37 -0800253static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000254 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000255 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
256 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000257 return false;
258 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000259 return true;
260}
261
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600262static bool destroy_dir(const std::string& dir) {
263 LOG(DEBUG) << "Destroying: " << dir;
264 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
265 PLOG(ERROR) << "Failed to destroy " << dir;
266 return false;
267 }
268 return true;
269}
270
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000271// NB this assumes that there is only one thread listening for crypt commands, because
272// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000273static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100274 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700275
276 if(is_wrapped_key_supported()) {
277 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
278 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
279 } else {
280 if (!android::vold::randomKey(&de_key)) return false;
281 if (!android::vold::randomKey(&ce_key)) return false;
282 }
283
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000284 if (create_ephemeral) {
285 // If the key should be created as ephemeral, don't store it.
286 s_ephemeral_users.insert(user_id);
287 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700288 auto const directory_path = get_ce_key_directory_path(user_id);
289 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
290 auto const paths = get_ce_key_paths(directory_path);
291 std::string ce_key_path;
292 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700293 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
294 ce_key))
295 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700296 fixate_user_ce_key(directory_path, ce_key_path, paths);
297 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700298 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700299 kEmptyAuthentication, de_key))
300 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100301 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700302
303 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000304 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000305 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700306
307 if (is_wrapped_key_supported()) {
308 KeyBuffer ephemeral_wrapped_de_key;
309 KeyBuffer ephemeral_wrapped_ce_key;
310
311 /* Export and install the DE keys */
312 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
313 LOG(ERROR) << "Failed to export de_key";
314 return false;
315 }
316 /* Export and install the CE keys */
317 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
318 LOG(ERROR) << "Failed to export de_key";
319 return false;
320 }
321
322 de_key = std::move(ephemeral_wrapped_de_key);
323 ce_key = std::move(ephemeral_wrapped_ce_key);
324 }
325 if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700326 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700327 s_ce_keys[user_id] = std::move(ce_key);
328
329 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000330 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700331
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000332 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000333 return true;
334}
335
Paul Crowleydf528a72016-03-09 09:31:37 -0800336static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
337 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000338 auto refi = key_map.find(user_id);
339 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700340 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000341 return false;
342 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800343 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000344 return true;
345}
346
Paul Crowley3aa914d2017-10-09 16:35:51 -0700347static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
Tom Cherry4c5bde22019-01-29 14:34:01 -0800348 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
349 if (entry == nullptr) {
350 return;
351 }
352 key_ref->contents_mode = entry->file_contents_mode;
353 key_ref->filenames_mode = entry->file_names_mode;
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700354}
355
Paul Crowley3aa914d2017-10-09 16:35:51 -0700356static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700357 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700358 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
359 key_ref.filenames_mode.c_str()) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100360}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100361
Paul Crowleydf528a72016-03-09 09:31:37 -0800362static bool is_numeric(const char* name) {
363 for (const char* p = name; *p != '\0'; p++) {
364 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000365 }
366 return true;
367}
368
369static bool load_all_de_keys() {
370 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800371 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000372 if (!dirp) {
373 PLOG(ERROR) << "Unable to read de key directory";
374 return false;
375 }
376 for (;;) {
377 errno = 0;
378 auto entry = readdir(dirp.get());
379 if (!entry) {
380 if (errno) {
381 PLOG(ERROR) << "Unable to read de key directory";
382 return false;
383 }
384 break;
385 }
386 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
387 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
388 continue;
389 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600390 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000391 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000392 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100393 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800394 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000395 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700396 if (is_wrapped_key_supported()) {
397 KeyBuffer ephemeral_wrapped_key;
398 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
399 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
400 return false;
401 }
402 key = std::move(ephemeral_wrapped_key);
403 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700404 if (!android::vold::installKey(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000405 s_de_key_raw_refs[user_id] = raw_ref;
406 LOG(DEBUG) << "Installed de key for user " << user_id;
407 }
408 }
Eric Biggersa701c452018-10-23 13:06:55 -0700409 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000410 // correct policy set on them, and that no rogue ones exist.
411 return true;
412}
413
Eric Biggersa701c452018-10-23 13:06:55 -0700414bool fscrypt_initialize_global_de() {
415 LOG(INFO) << "fscrypt_initialize_global_de";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700416 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800417
Paul Crowley38132a12016-02-09 09:50:32 +0000418 if (s_global_de_initialized) {
419 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000420 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800421 }
422
Paul Crowley3aa914d2017-10-09 16:35:51 -0700423 PolicyKeyRef device_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700424 wrapped_key_supported = is_wrapped_key_supported();
425
426 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication,
427 device_key_path, device_key_temp,
428 &device_ref.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700429 return false;
430 get_data_file_encryption_modes(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800431
Paul Crowley3aa914d2017-10-09 16:35:51 -0700432 std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
Eric Biggersa701c452018-10-23 13:06:55 -0700433 std::string mode_filename = std::string("/data") + fscrypt_key_mode;
Tommy Chiua98464f2019-03-26 14:14:19 +0800434 if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700435
Eric Biggersa701c452018-10-23 13:06:55 -0700436 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Tommy Chiua98464f2019-03-26 14:14:19 +0800437 if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
438
Paul Crowleyf71ace32016-06-02 11:01:19 -0700439 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800440
Tommy Chiua98464f2019-03-26 14:14:19 +0800441 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowley38132a12016-02-09 09:50:32 +0000442 s_global_de_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000443 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800444}
445
Eric Biggersa701c452018-10-23 13:06:55 -0700446bool fscrypt_init_user0() {
447 LOG(DEBUG) << "fscrypt_init_user0";
448 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000449 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
450 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
451 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700452 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000453 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000454 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700455 // TODO: switch to loading only DE_0 here once framework makes
456 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000457 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000458 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700459 // We can only safely prepare DE storage here, since CE keys are probably
460 // entangled with user credentials. The framework will always prepare CE
461 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700462 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700463 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000464 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700465 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700466
467 // If this is a non-FBE device that recently left an emulated mode,
468 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700469 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
470 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700471 }
472
Paul Crowley76107cb2016-02-09 10:04:39 +0000473 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000474}
475
Eric Biggersa701c452018-10-23 13:06:55 -0700476bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
477 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
478 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000479 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000480 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000481 // FIXME test for existence of key that is not loaded yet
482 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700483 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800484 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000485 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000486 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800487 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000488 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000489 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000490 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000491 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800492}
493
Pavel Grafovb350ed02017-07-27 17:34:57 +0100494static void drop_caches() {
495 // Clean any dirty pages (otherwise they won't be dropped).
496 sync();
497 // Drop inode and page caches.
Tommy Chiua98464f2019-03-26 14:14:19 +0800498 if (!writeStringToFile("3", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100499 PLOG(ERROR) << "Failed to drop caches during key eviction";
500 }
501}
502
Andrew Scull7ec25c72016-10-31 10:28:25 +0000503static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000504 bool success = true;
505 std::string raw_ref;
506 // If we haven't loaded the CE key, no need to evict it.
507 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700508 success &= android::vold::evictKey(raw_ref);
Pavel Grafovb350ed02017-07-27 17:34:57 +0100509 drop_caches();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000510 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700511
512 if(is_wrapped_key_supported()) {
513 KeyBuffer key;
514 key = s_ce_keys[user_id];
515
516 std::string keystr(key.data(), key.size());
517 Keymaster keymaster;
518
519 if (!keymaster) {
520 s_ce_keys.erase(user_id);
521 s_ce_key_raw_refs.erase(user_id);
522 return false;
523 }
524 keymaster.deleteKey(keystr);
525 }
526
527 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000528 s_ce_key_raw_refs.erase(user_id);
529 return success;
530}
531
Eric Biggersa701c452018-10-23 13:06:55 -0700532bool fscrypt_destroy_user_key(userid_t user_id) {
533 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
534 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000535 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000536 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000537 bool success = true;
538 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000539 success &= evict_ce_key(user_id);
Paul Crowley14c8c072018-09-18 13:30:21 -0700540 success &=
541 lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000542 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000543 auto it = s_ephemeral_users.find(user_id);
544 if (it != s_ephemeral_users.end()) {
545 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000546 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700547 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700548 success &= android::vold::destroyKey(path);
549 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700550 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700551 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700552 success &= android::vold::destroyKey(de_key_path);
553 } else {
554 LOG(INFO) << "Not present so not erasing: " << de_key_path;
555 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100556 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000557 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100558}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800559
Paul Crowley76107cb2016-02-09 10:04:39 +0000560static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700561 if (chmod(path.c_str(), 0000) != 0) {
562 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000563 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700564 }
565#if EMULATED_USES_SELINUX
566 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
567 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000568 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700569 }
570#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000571 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700572}
573
Paul Crowley76107cb2016-02-09 10:04:39 +0000574static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700575 if (chmod(path.c_str(), mode) != 0) {
576 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000577 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700578 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700579 }
580#if EMULATED_USES_SELINUX
581 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
582 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000583 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700584 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700585 }
586#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000587 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700588}
589
Paul Crowley3b71fc52017-10-09 10:55:21 -0700590static bool parse_hex(const std::string& hex, std::string* result) {
591 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800592 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000593 return true;
594 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800595 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800596 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000597 return false;
598 }
599 return true;
600}
601
Paul Crowley3aa914d2017-10-09 16:35:51 -0700602static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
603 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
604}
605
Paul Crowley26a53882017-10-26 11:16:39 -0700606static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
607 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
608}
609
Paul Crowley3aa914d2017-10-09 16:35:51 -0700610static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700611 PolicyKeyRef* key_ref, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700612 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
613 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700614 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700615 if (android::vold::pathExists(secdiscardable_path)) {
616 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
617 return false;
618 } else {
619 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
620 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
621 return false;
622 }
623 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
624 return false;
625 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700626 auto key_path = volkey_path(misc_path, volume_uuid);
627 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
628 PLOG(ERROR) << "Creating directories for: " << key_path;
629 return false;
630 }
Paul Crowley26a53882017-10-26 11:16:39 -0700631 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700632 wrapped_key_supported = is_wrapped_key_supported_external(flags);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700633
Paul Crowley26a53882017-10-26 11:16:39 -0700634 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700635 &key_ref->key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700636 return false;
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700637
638 if (is_ice_supported_external(flags)) {
639 key_ref->contents_mode =
640 android::base::GetProperty("ro.crypto.volume.contents_mode", "ice");
641 } else {
642 key_ref->contents_mode =
643 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
644 }
645
Paul Crowley3aa914d2017-10-09 16:35:51 -0700646 key_ref->filenames_mode =
647 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
648 return true;
649}
650
651static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700652 auto path = volkey_path(misc_path, volume_uuid);
653 if (!android::vold::pathExists(path)) return true;
654 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700655}
656
Eric Biggersa701c452018-10-23 13:06:55 -0700657bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700658 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700659 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700660 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700661 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000662 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700663 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800664 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700665 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700666 auto auth =
667 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowleya3630362016-05-17 14:17:56 -0700668 auto const directory_path = get_ce_key_directory_path(user_id);
669 auto const paths = get_ce_key_paths(directory_path);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700670
671 KeyBuffer ce_key;
672 if(is_wrapped_key_supported()) {
673 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
674 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
675 LOG(DEBUG) << "Successfully retrieved key";
676 } else {
677 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
678 LOG(DEBUG) << "Successfully retrieved key";
679 }
680 }
681 } else {
682 auto it = s_ce_keys.find(user_id);
683 if (it == s_ce_keys.end()) {
684 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
685 return false;
686 }
687 ce_key = it->second;
688 }
689
Paul Crowleya3630362016-05-17 14:17:56 -0700690 std::string ce_key_path;
691 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700692 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800693 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700694 return true;
695}
696
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800697bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700698 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800699 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700700 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800701 if (!fscrypt_is_native()) return true;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700702 if (s_ephemeral_users.count(user_id) != 0) return true;
703 std::string token, secret;
704
705 if (!parse_hex(token_hex, &token)) return false;
706 if (!parse_hex(secret_hex, &secret)) return false;
707
708 if (is_wrapped_key_supported()) {
709 auto const directory_path = get_ce_key_directory_path(user_id);
710 auto const paths = get_ce_key_paths(directory_path);
711
712 KeyBuffer ce_key;
713 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
714
715 auto auth = android::vold::KeyAuthentication(token, secret);
716 /* Retrieve key while removing a pin. A secret is needed */
717 if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) {
718 LOG(DEBUG) << "Successfully retrieved key";
719 } else {
720 /* Retrieve key when going None to swipe and vice versa when a
721 synthetic password is present */
722 if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
723 LOG(DEBUG) << "Successfully retrieved key";
724 }
725 }
726
727 std::string ce_key_path;
728 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
729 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key))
730 return false;
731 } else {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800732 if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700733 }
734 return true;
735}
736
Eric Biggersa701c452018-10-23 13:06:55 -0700737bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
738 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
739 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700740 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700741 auto const directory_path = get_ce_key_directory_path(user_id);
742 auto const paths = get_ce_key_paths(directory_path);
743 if (paths.empty()) {
744 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
745 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000746 }
Paul Crowleya3630362016-05-17 14:17:56 -0700747 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000748 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000749}
750
Jeff Sharkey47695b22016-02-01 17:02:29 -0700751// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700752bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700753 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700754 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700755 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700756 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000757 if (s_ce_key_raw_refs.count(user_id) != 0) {
758 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000759 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000760 }
761 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800762 if (!parse_hex(token_hex, &token)) return false;
763 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000764 android::vold::KeyAuthentication auth(token, secret);
765 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000766 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000767 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800768 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700769 } else {
770 // When in emulation mode, we just use chmod. However, we also
771 // unlock directories when not in emulation mode, to bring devices
772 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000773 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800774 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700775 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
776 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700777 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000778 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700779 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800780 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000781 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800782}
783
Jeff Sharkey47695b22016-02-01 17:02:29 -0700784// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700785bool fscrypt_lock_user_key(userid_t user_id) {
786 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
787 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000788 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700789 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800790 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000791 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800792 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700793 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
794 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000795 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000796 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800797 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800798 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700799
Paul Crowley76107cb2016-02-09 10:04:39 +0000800 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800801}
802
Paul Crowley82b41ff2017-10-20 08:17:54 -0700803static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
804 userid_t user_id, int flags) {
805 if (0 != android::vold::ForkExecvp(
806 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
807 std::to_string(user_id), std::to_string(flags)})) {
808 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700809 return false;
810 }
811 return true;
812}
813
Eric Biggersa701c452018-10-23 13:06:55 -0700814bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700815 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700816 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800817 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700818
Paul Crowley82b41ff2017-10-20 08:17:54 -0700819 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600820 // DE_sys key
821 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
822 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
823 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600824
825 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700826 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
827 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800828 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700829 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
830
Paul Crowley3b71fc52017-10-09 10:55:21 -0700831 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700832 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600833#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700834 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700835 multiuser_get_uid(user_id, AID_EVERYBODY)))
836 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600837#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700838 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600839
Paul Crowley6b756ce2017-10-05 14:07:09 -0700840 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
841 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800842 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700843 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000844 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000845
Eric Biggersa701c452018-10-23 13:06:55 -0700846 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700847 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700848 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700849 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
850 get_data_file_encryption_modes(&de_ref);
851 if (!ensure_policy(de_ref, system_de_path)) return false;
852 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800853 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700854 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700855 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700856 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700857 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700858 }
Paul Crowley285956f2016-01-20 13:12:38 +0000859 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800860
Paul Crowley82b41ff2017-10-20 08:17:54 -0700861 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600862 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700863 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
864 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800865 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600866 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
867 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800868
Paul Crowley3b71fc52017-10-09 10:55:21 -0700869 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700870 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
871 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800872 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700873 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000874 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
875 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700876
Eric Biggersa701c452018-10-23 13:06:55 -0700877 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700878 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700879 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700880 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
881 get_data_file_encryption_modes(&ce_ref);
882 if (!ensure_policy(ce_ref, system_ce_path)) return false;
883 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800884 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700885
Paul Crowley3aa914d2017-10-09 16:35:51 -0700886 } else {
Gaurav Kashyap38cbb942019-07-17 18:11:57 -0700887 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700888 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700889 if (!ensure_policy(ce_ref, media_ce_path)) return false;
890 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700891 }
Paul Crowley1a965262017-10-13 13:49:50 -0700892
893 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700894 // Now that credentials have been installed, we can run restorecon
895 // over these paths
896 // NOTE: these paths need to be kept in sync with libselinux
897 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700898 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700899 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700900 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800901 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700902 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800903
Paul Crowley76107cb2016-02-09 10:04:39 +0000904 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800905}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600906
Eric Biggersa701c452018-10-23 13:06:55 -0700907bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
908 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600909 << ", user " << user_id << ", flags " << flags;
910 bool res = true;
911
Paul Crowley82b41ff2017-10-20 08:17:54 -0700912 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
913
914 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700915 // CE_n key
916 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
917 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800918 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700919 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
920 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
921
922 res &= destroy_dir(media_ce_path);
923 res &= destroy_dir(user_ce_path);
924 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700925 res &= destroy_dir(system_ce_path);
926 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800927 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700928 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700929 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700930 res &= destroy_volkey(misc_ce_path, volume_uuid);
931 }
Paul Crowley8e550662017-10-16 17:01:44 -0700932 }
933 }
934
Paul Crowley82b41ff2017-10-20 08:17:54 -0700935 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600936 // DE_sys key
937 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
938 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
939 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600940
941 // DE_n key
942 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
943 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800944 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600945 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
946
Paul Crowley8e550662017-10-16 17:01:44 -0700947 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700948 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600949 res &= destroy_dir(system_legacy_path);
950#if MANAGE_MISC_DIRS
951 res &= destroy_dir(misc_legacy_path);
952#endif
953 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600954 res &= destroy_dir(system_de_path);
955 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800956 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700957 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700958 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700959 res &= destroy_volkey(misc_de_path, volume_uuid);
960 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600961 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600962 }
963
964 return res;
965}
Rubin Xu2436e272017-04-27 20:43:10 +0100966
Paul Crowleyc6433a22017-10-24 14:54:43 -0700967static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
968 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
969 if (!dirp) {
970 PLOG(ERROR) << "Unable to open directory: " + directory_path;
971 return false;
972 }
973 bool res = true;
974 for (;;) {
975 errno = 0;
976 auto const entry = readdir(dirp.get());
977 if (!entry) {
978 if (errno) {
979 PLOG(ERROR) << "Unable to read directory: " + directory_path;
980 return false;
981 }
982 break;
983 }
984 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
985 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
986 continue;
987 }
988 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
989 }
990 return res;
991}
992
Eric Biggersa701c452018-10-23 13:06:55 -0700993bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700994 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700995 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700996 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
997 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700998 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
999 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
1000 return res;
1001}