blob: a491206e3d1c71ee18e0f688d9eb8aec98e1deeb [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 Crowley05720802016-02-08 15:55:41 +000067using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010068using android::vold::KeyBuffer;
Shivaprasad Hongal92292622018-07-05 14:49:12 -070069using android::vold::Keymaster;
70using android::hardware::keymaster::V4_0::KeyFormat;
Tommy Chiua98464f2019-03-26 14:14:19 +080071using android::vold::writeStringToFile;
Paul Crowley5e53ff62019-10-24 14:55:17 -070072using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080073
Paul Lawrence731a7a22015-04-28 22:14:15 +000074namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000075
Eric Biggersa701c452018-10-23 13:06:55 -070076const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080077const std::string device_key_path = device_key_dir + "/key";
78const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080079
Paul Crowleydf528a72016-03-09 09:31:37 -080080const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
81const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070082const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000083
Paul Crowley26a53882017-10-26 11:16:39 -070084const std::string systemwide_volume_key_dir =
85 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
86
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070087bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080088
Paul Crowleydf528a72016-03-09 09:31:37 -080089// Some users are ephemeral, don't try to wipe their keys from disk
90std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080091
Paul Crowleydf528a72016-03-09 09:31:37 -080092// Map user ids to key references
93std::map<userid_t, std::string> s_de_key_raw_refs;
94std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Lawrence731a7a22015-04-28 22:14:15 +000095
Paul Crowley14c8c072018-09-18 13:30:21 -070096} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000097
Eric Biggersa701c452018-10-23 13:06:55 -070098static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +000099 return property_get_bool("persist.sys.emulate_fbe", false);
100}
101
Paul Crowley3b71fc52017-10-09 10:55:21 -0700102static const char* escape_empty(const std::string& value) {
103 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000104}
105
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000106static std::string get_de_key_path(userid_t user_id) {
107 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
108}
109
Paul Crowleya3630362016-05-17 14:17:56 -0700110static std::string get_ce_key_directory_path(userid_t user_id) {
111 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
112}
113
114// Returns the keys newest first
115static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
116 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
117 if (!dirp) {
118 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
119 return std::vector<std::string>();
120 }
121 std::vector<std::string> result;
122 for (;;) {
123 errno = 0;
124 auto const entry = readdir(dirp.get());
125 if (!entry) {
126 if (errno) {
127 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
128 return std::vector<std::string>();
129 }
130 break;
131 }
132 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
133 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
134 continue;
135 }
136 result.emplace_back(directory_path + "/" + entry->d_name);
137 }
138 std::sort(result.begin(), result.end());
139 std::reverse(result.begin(), result.end());
140 return result;
141}
142
143static std::string get_ce_key_current_path(const std::string& directory_path) {
144 return directory_path + "/current";
145}
146
147static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700148 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700149 if (paths.empty()) {
150 *ce_key_path = get_ce_key_current_path(directory_path);
151 return true;
152 }
153 for (unsigned int i = 0; i < UINT_MAX; i++) {
154 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
155 if (paths[0] < candidate) {
156 *ce_key_path = candidate;
157 return true;
158 }
159 }
160 return false;
161}
162
163// Discard all keys but the named one; rename it to canonical name.
164// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700165static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700166 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700167 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700168 if (other_path != to_fix) {
169 android::vold::destroyKey(other_path);
170 }
171 }
172 auto const current_path = get_ce_key_current_path(directory_path);
173 if (to_fix != current_path) {
174 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
175 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
176 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800177 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700178 }
179 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800180 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700181}
182
183static bool read_and_fixate_user_ce_key(userid_t user_id,
184 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700185 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700186 auto const directory_path = get_ce_key_directory_path(user_id);
187 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700188 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700189 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
190 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
191 LOG(DEBUG) << "Successfully retrieved key";
192 fixate_user_ce_key(directory_path, ce_key_path, paths);
193 return true;
194 }
195 }
196 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
197 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100198}
199
Eric Biggers83a73d72019-09-30 13:06:47 -0700200// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700201static void get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700202 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
203 if (entry == nullptr) {
204 return;
205 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700206 if (!ParseOptions(entry->encryption_options, options)) {
207 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
208 << entry->encryption_options;
209 return;
210 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700211}
212
213// Retrieve the version to use for encryption policies on the /data filesystem.
214static int get_data_file_policy_version(void) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700215 EncryptionOptions options;
216 get_data_file_encryption_options(&options);
217 return options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700218}
219
220// Retrieve the options to use for encryption policies on adoptable storage.
Steven Laverd4eb3412019-11-09 19:05:21 -0800221static bool get_volume_file_encryption_options(EncryptionOptions* options, int flags) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700222 auto contents_mode =
Barani Muthukumaranff525592020-01-29 14:38:31 -0800223 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700224 auto filenames_mode =
Eric Biggers83a73d72019-09-30 13:06:47 -0700225 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700226 return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
227 contents_mode + ":" + filenames_mode + ":v1"),
228 options);
Eric Biggers83a73d72019-09-30 13:06:47 -0700229}
230
Eric Biggersf3dc4202019-09-30 13:05:58 -0700231// Install a key for use by encrypted files on the /data filesystem.
232static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700233 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700234}
235
236// Evict a key for use by encrypted files on the /data filesystem.
237static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700238 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700239}
240
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700241bool is_wrapped_key_supported() {
Yifan Hong804afe12019-02-07 12:56:47 -0800242 return GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700243}
244
Barani Muthukumaranff525592020-01-29 14:38:31 -0800245static bool is_wrapped_key_supported_external() {
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700246 return false;
247}
248
Neeraj Sonic480f912018-12-14 15:18:15 +0530249bool is_metadata_wrapped_key_supported() {
250 return GetEntryForMountPoint(&fstab_default, METADATA_MNT_POINT)->fs_mgr_flags.wrapped_key;
251}
252
Paul Crowleydf528a72016-03-09 09:31:37 -0800253static bool read_and_install_user_ce_key(userid_t user_id,
254 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000255 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100256 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700257 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000258 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700259
260 if (is_wrapped_key_supported()) {
261 KeyBuffer ephemeral_wrapped_key;
262 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) {
263 LOG(ERROR) << "Failed to export ce key";
264 return false;
265 }
266 ce_key = std::move(ephemeral_wrapped_key);
267 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700268 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000269 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000270 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000271 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000272}
273
Paul Crowleydf528a72016-03-09 09:31:37 -0800274static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000275 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000276 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
277 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000278 return false;
279 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000280 return true;
281}
282
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600283static bool destroy_dir(const std::string& dir) {
284 LOG(DEBUG) << "Destroying: " << dir;
285 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
286 PLOG(ERROR) << "Failed to destroy " << dir;
287 return false;
288 }
289 return true;
290}
291
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000292// NB this assumes that there is only one thread listening for crypt commands, because
293// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000294static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100295 KeyBuffer de_key, ce_key;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700296
297 if(is_wrapped_key_supported()) {
298 if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false;
299 if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false;
300 } else {
301 if (!android::vold::randomKey(&de_key)) return false;
302 if (!android::vold::randomKey(&ce_key)) return false;
303 }
304
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000305 if (create_ephemeral) {
306 // If the key should be created as ephemeral, don't store it.
307 s_ephemeral_users.insert(user_id);
308 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700309 auto const directory_path = get_ce_key_directory_path(user_id);
310 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
311 auto const paths = get_ce_key_paths(directory_path);
312 std::string ce_key_path;
313 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700314 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
315 ce_key))
316 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700317 fixate_user_ce_key(directory_path, ce_key_path, paths);
318 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700319 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700320 kEmptyAuthentication, de_key))
321 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100322 }
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700323
324 /* Install the DE keys */
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000325 std::string de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000326 std::string ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700327
328 if (is_wrapped_key_supported()) {
329 KeyBuffer ephemeral_wrapped_de_key;
330 KeyBuffer ephemeral_wrapped_ce_key;
331
332 /* Export and install the DE keys */
333 if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) {
334 LOG(ERROR) << "Failed to export de_key";
335 return false;
336 }
337 /* Export and install the CE keys */
338 if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) {
339 LOG(ERROR) << "Failed to export de_key";
340 return false;
341 }
342
343 de_key = std::move(ephemeral_wrapped_de_key);
344 ce_key = std::move(ephemeral_wrapped_ce_key);
345 }
Steven Lavered747be2019-10-24 16:40:01 -0700346 if (!install_data_key(de_key, &de_raw_ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700347 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700348
349 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000350 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700351
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000352 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000353 return true;
354}
355
Paul Crowleydf528a72016-03-09 09:31:37 -0800356static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
357 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000358 auto refi = key_map.find(user_id);
359 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700360 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000361 return false;
362 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800363 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000364 return true;
365}
366
Paul Crowleydf528a72016-03-09 09:31:37 -0800367static bool is_numeric(const char* name) {
368 for (const char* p = name; *p != '\0'; p++) {
369 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000370 }
371 return true;
372}
373
374static bool load_all_de_keys() {
375 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800376 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000377 if (!dirp) {
378 PLOG(ERROR) << "Unable to read de key directory";
379 return false;
380 }
381 for (;;) {
382 errno = 0;
383 auto entry = readdir(dirp.get());
384 if (!entry) {
385 if (errno) {
386 PLOG(ERROR) << "Unable to read de key directory";
387 return false;
388 }
389 break;
390 }
391 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
392 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
393 continue;
394 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600395 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000396 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000397 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100398 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800399 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000400 std::string raw_ref;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700401 if (is_wrapped_key_supported()) {
402 KeyBuffer ephemeral_wrapped_key;
403 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
404 LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys";
405 return false;
406 }
407 key = std::move(ephemeral_wrapped_key);
408 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700409 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000410 s_de_key_raw_refs[user_id] = raw_ref;
411 LOG(DEBUG) << "Installed de key for user " << user_id;
412 }
413 }
Eric Biggersa701c452018-10-23 13:06:55 -0700414 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000415 // correct policy set on them, and that no rogue ones exist.
416 return true;
417}
418
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700419bool fscrypt_initialize_systemwide_keys() {
420 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700421 bool wrapped_key_supported = false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800422
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700423 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000424 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000425 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800426 }
427
Paul Crowley5e53ff62019-10-24 14:55:17 -0700428 EncryptionPolicy device_policy;
429 get_data_file_encryption_options(&device_policy.options);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700430 wrapped_key_supported = is_wrapped_key_supported();
431
Eric Biggers83a73d72019-09-30 13:06:47 -0700432 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700433 device_key_temp, "", device_policy.options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800434 &device_policy.key_raw_ref, wrapped_key_supported))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700435 return false;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800436
Paul Crowley5e53ff62019-10-24 14:55:17 -0700437 std::string options_string;
438 if (!OptionsToString(device_policy.options, &options_string)) {
439 LOG(ERROR) << "Unable to serialize options";
440 return false;
441 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700442 std::string options_filename = std::string("/data") + fscrypt_key_mode;
443 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700444
Eric Biggersa701c452018-10-23 13:06:55 -0700445 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700446 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700447 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800448
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700449 KeyBuffer per_boot_key;
450 if (!android::vold::randomKey(&per_boot_key)) return false;
451 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700452 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700453 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
454 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
455 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
456
Tommy Chiua98464f2019-03-26 14:14:19 +0800457 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700458 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000459 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800460}
461
Eric Biggersa701c452018-10-23 13:06:55 -0700462bool fscrypt_init_user0() {
463 LOG(DEBUG) << "fscrypt_init_user0";
464 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000465 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
466 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
467 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700468 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000469 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000470 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700471 // TODO: switch to loading only DE_0 here once framework makes
472 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000473 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000474 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700475 // We can only safely prepare DE storage here, since CE keys are probably
476 // entangled with user credentials. The framework will always prepare CE
477 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700478 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700479 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000480 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700481 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700482
483 // If this is a non-FBE device that recently left an emulated mode,
484 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700485 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
486 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700487 }
488
Paul Crowley76107cb2016-02-09 10:04:39 +0000489 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000490}
491
Eric Biggersa701c452018-10-23 13:06:55 -0700492bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
493 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
494 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000495 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000496 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000497 // FIXME test for existence of key that is not loaded yet
498 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700499 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800500 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000501 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000502 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800503 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000504 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000505 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000506 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000507 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800508}
509
Eric Biggersce368682019-04-03 15:44:06 -0700510// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700511// in the case where the keys are being put in the session keyring (rather in
512// the newer filesystem-level keyrings), because removing a key from the session
513// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
514// was already set up. So to remove the per-file keys and make the files
515// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700516//
517// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
518// objects systemwide. This is overkill, but it's the best available method
519// currently. Don't use drop_caches mode "3" because that also evicts pagecache
520// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700521static void drop_caches_if_needed() {
522 if (android::vold::isFsKeyringSupported()) {
523 return;
524 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100525 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700526 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100527 PLOG(ERROR) << "Failed to drop caches during key eviction";
528 }
529}
530
Andrew Scull7ec25c72016-10-31 10:28:25 +0000531static bool evict_ce_key(userid_t user_id) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000532 bool success = true;
533 std::string raw_ref;
534 // If we haven't loaded the CE key, no need to evict it.
535 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700536 success &= evict_data_key(raw_ref);
537 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000538 }
Shivaprasad Hongala8ae0122018-10-10 16:05:33 -0700539
Andrew Scull7ec25c72016-10-31 10:28:25 +0000540 s_ce_key_raw_refs.erase(user_id);
541 return success;
542}
543
Eric Biggersa701c452018-10-23 13:06:55 -0700544bool fscrypt_destroy_user_key(userid_t user_id) {
545 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
546 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000547 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000548 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000549 bool success = true;
550 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000551 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700552 success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_data_key(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000553 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000554 auto it = s_ephemeral_users.find(user_id);
555 if (it != s_ephemeral_users.end()) {
556 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000557 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700558 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700559 success &= android::vold::destroyKey(path);
560 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700561 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700562 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700563 success &= android::vold::destroyKey(de_key_path);
564 } else {
565 LOG(INFO) << "Not present so not erasing: " << de_key_path;
566 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100567 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000568 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100569}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800570
Paul Crowley76107cb2016-02-09 10:04:39 +0000571static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700572 if (chmod(path.c_str(), 0000) != 0) {
573 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000574 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700575 }
576#if EMULATED_USES_SELINUX
577 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
578 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000579 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700580 }
581#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000582 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700583}
584
Paul Crowley76107cb2016-02-09 10:04:39 +0000585static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700586 if (chmod(path.c_str(), mode) != 0) {
587 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000588 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700589 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700590 }
591#if EMULATED_USES_SELINUX
592 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
593 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000594 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700595 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700596 }
597#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000598 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700599}
600
Paul Crowley3b71fc52017-10-09 10:55:21 -0700601static bool parse_hex(const std::string& hex, std::string* result) {
602 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800603 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000604 return true;
605 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800606 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800607 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000608 return false;
609 }
610 return true;
611}
612
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700613static std::optional<android::vold::KeyAuthentication> authentication_from_hex(
614 const std::string& token_hex, const std::string& secret_hex) {
615 std::string token, secret;
616 if (!parse_hex(token_hex, &token)) return std::optional<android::vold::KeyAuthentication>();
617 if (!parse_hex(secret_hex, &secret)) return std::optional<android::vold::KeyAuthentication>();
618 if (secret.empty()) {
619 return kEmptyAuthentication;
620 } else {
621 return android::vold::KeyAuthentication(token, secret);
622 }
623}
624
Paul Crowley3aa914d2017-10-09 16:35:51 -0700625static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
626 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
627}
628
Paul Crowley26a53882017-10-26 11:16:39 -0700629static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
630 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
631}
632
Paul Crowley3aa914d2017-10-09 16:35:51 -0700633static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Steven Laverd4eb3412019-11-09 19:05:21 -0800634 EncryptionPolicy* policy, int flags) {
Paul Crowley26a53882017-10-26 11:16:39 -0700635 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
636 std::string secdiscardable_hash;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700637 bool wrapped_key_supported = false;
Paul Crowley26a53882017-10-26 11:16:39 -0700638 if (android::vold::pathExists(secdiscardable_path)) {
639 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
640 return false;
641 } else {
642 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
643 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
644 return false;
645 }
646 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
647 return false;
648 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700649 auto key_path = volkey_path(misc_path, volume_uuid);
650 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
651 PLOG(ERROR) << "Creating directories for: " << key_path;
652 return false;
653 }
Paul Crowley26a53882017-10-26 11:16:39 -0700654 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700655
Steven Laverd4eb3412019-11-09 19:05:21 -0800656 if (!get_volume_file_encryption_options(&policy->options, flags)) return false;
Barani Muthukumaranff525592020-01-29 14:38:31 -0800657 wrapped_key_supported = is_wrapped_key_supported_external();
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700658
Eric Biggers83a73d72019-09-30 13:06:47 -0700659 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley5e53ff62019-10-24 14:55:17 -0700660 volume_uuid, policy->options.version,
Steven Laverd4eb3412019-11-09 19:05:21 -0800661 &policy->key_raw_ref, wrapped_key_supported);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700662}
663
664static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700665 auto path = volkey_path(misc_path, volume_uuid);
666 if (!android::vold::pathExists(path)) return true;
667 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700668}
669
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700670static bool fscrypt_rewrap_user_key(userid_t user_id, int serial,
671 const android::vold::KeyAuthentication& retrieve_auth,
672 const android::vold::KeyAuthentication& store_auth) {
673 if (s_ephemeral_users.count(user_id) != 0) return true;
674 auto const directory_path = get_ce_key_directory_path(user_id);
675 KeyBuffer ce_key;
676 std::string ce_key_current_path = get_ce_key_current_path(directory_path);
677 if (android::vold::retrieveKey(ce_key_current_path, retrieve_auth, &ce_key)) {
678 LOG(DEBUG) << "Successfully retrieved key";
679 // TODO(147732812): Remove this once Locksettingservice is fixed.
680 // Currently it calls fscrypt_clear_user_key_auth with a secret when lockscreen is
681 // changed from swipe to none or vice-versa
682 } else if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) {
683 LOG(DEBUG) << "Successfully retrieved key with empty auth";
684 } else {
685 LOG(ERROR) << "Failed to retrieve key for user " << user_id;
686 return false;
687 }
688 auto const paths = get_ce_key_paths(directory_path);
Steven Laver4771d982020-01-23 17:03:42 -0800689
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700690 std::string ce_key_path;
691 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
692 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, store_auth, ce_key))
693 return false;
694 if (!android::vold::FsyncDirectory(directory_path)) return false;
695 return true;
696}
697
Eric Biggersa701c452018-10-23 13:06:55 -0700698bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700699 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700700 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700701 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700702 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, kEmptyAuthentication, *auth);
Paul Crowleya3630362016-05-17 14:17:56 -0700706}
707
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800708bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700709 const std::string& secret_hex) {
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800710 LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700711 << " token_present=" << (token_hex != "!");
Diego Wilsondd0a81e2018-11-20 11:38:50 -0800712 if (!fscrypt_is_native()) return true;
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700713 auto auth = authentication_from_hex(token_hex, secret_hex);
714 if (!auth) return false;
715 return fscrypt_rewrap_user_key(user_id, serial, *auth, kEmptyAuthentication);
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700716}
717
Eric Biggersa701c452018-10-23 13:06:55 -0700718bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
719 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
720 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700721 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700722 auto const directory_path = get_ce_key_directory_path(user_id);
723 auto const paths = get_ce_key_paths(directory_path);
724 if (paths.empty()) {
725 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
726 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000727 }
Paul Crowleya3630362016-05-17 14:17:56 -0700728 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000729 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000730}
731
Jeff Sharkey47695b22016-02-01 17:02:29 -0700732// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700733bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700734 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700735 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700736 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700737 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000738 if (s_ce_key_raw_refs.count(user_id) != 0) {
739 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000740 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000741 }
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700742 auto auth = authentication_from_hex(token_hex, secret_hex);
743 if (!auth) return false;
744 if (!read_and_install_user_ce_key(user_id, *auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000745 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000746 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800747 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700748 } else {
749 // When in emulation mode, we just use chmod. However, we also
750 // unlock directories when not in emulation mode, to bring devices
751 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000752 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800753 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700754 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
755 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700756 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000757 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700758 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800759 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000760 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800761}
762
Jeff Sharkey47695b22016-02-01 17:02:29 -0700763// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700764bool fscrypt_lock_user_key(userid_t user_id) {
765 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
766 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000767 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700768 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800769 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000770 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800771 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700772 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
773 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000774 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000775 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800776 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800777 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700778
Paul Crowley76107cb2016-02-09 10:04:39 +0000779 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800780}
781
Paul Crowley82b41ff2017-10-20 08:17:54 -0700782static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
783 userid_t user_id, int flags) {
784 if (0 != android::vold::ForkExecvp(
785 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
786 std::to_string(user_id), std::to_string(flags)})) {
787 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700788 return false;
789 }
790 return true;
791}
792
Eric Biggersa701c452018-10-23 13:06:55 -0700793bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700794 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700795 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800796 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700797
Paul Crowley82b41ff2017-10-20 08:17:54 -0700798 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600799 // DE_sys key
800 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
801 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
802 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600803
804 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700805 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
806 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800807 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700808 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
809
Paul Crowley3b71fc52017-10-09 10:55:21 -0700810 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700811 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600812#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700813 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700814 multiuser_get_uid(user_id, AID_EVERYBODY)))
815 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600816#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700817 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600818
Paul Crowley6b756ce2017-10-05 14:07:09 -0700819 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
820 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800821 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700822 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000823 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000824
Eric Biggersa701c452018-10-23 13:06:55 -0700825 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700826 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700827 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700828 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
829 return false;
830 get_data_file_encryption_options(&de_policy.options);
831 if (!EnsurePolicy(de_policy, system_de_path)) return false;
832 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
833 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700834 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800835 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700836 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700837 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700838 }
Paul Crowley285956f2016-01-20 13:12:38 +0000839 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800840
Paul Crowley82b41ff2017-10-20 08:17:54 -0700841 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600842 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700843 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
844 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800845 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600846 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
847 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800848
Paul Crowley3b71fc52017-10-09 10:55:21 -0700849 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700850 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
851 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800852 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700853 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000854 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
855 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700856
Eric Biggersa701c452018-10-23 13:06:55 -0700857 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700858 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700859 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700860 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
861 return false;
862 get_data_file_encryption_options(&ce_policy.options);
863 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
864 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
865 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700866
Paul Crowley3aa914d2017-10-09 16:35:51 -0700867 } else {
Steven Laverd4eb3412019-11-09 19:05:21 -0800868 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700869 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700870 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
871 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700872 }
Paul Crowley1a965262017-10-13 13:49:50 -0700873
874 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700875 // Now that credentials have been installed, we can run restorecon
876 // over these paths
877 // NOTE: these paths need to be kept in sync with libselinux
878 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700879 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700880 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700881 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800882 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700883 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800884
Paul Crowley76107cb2016-02-09 10:04:39 +0000885 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800886}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600887
Eric Biggersa701c452018-10-23 13:06:55 -0700888bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
889 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600890 << ", user " << user_id << ", flags " << flags;
891 bool res = true;
892
Paul Crowley82b41ff2017-10-20 08:17:54 -0700893 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
894
895 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700896 // CE_n key
897 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
898 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800899 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700900 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
901 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
902
903 res &= destroy_dir(media_ce_path);
904 res &= destroy_dir(user_ce_path);
905 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700906 res &= destroy_dir(system_ce_path);
907 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800908 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700909 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700910 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700911 res &= destroy_volkey(misc_ce_path, volume_uuid);
912 }
Paul Crowley8e550662017-10-16 17:01:44 -0700913 }
914 }
915
Paul Crowley82b41ff2017-10-20 08:17:54 -0700916 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600917 // DE_sys key
918 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
919 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
920 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600921
922 // DE_n key
923 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
924 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800925 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600926 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
927
Paul Crowley8e550662017-10-16 17:01:44 -0700928 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700929 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600930 res &= destroy_dir(system_legacy_path);
931#if MANAGE_MISC_DIRS
932 res &= destroy_dir(misc_legacy_path);
933#endif
934 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600935 res &= destroy_dir(system_de_path);
936 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800937 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700938 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700939 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700940 res &= destroy_volkey(misc_de_path, volume_uuid);
941 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600942 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600943 }
944
945 return res;
946}
Rubin Xu2436e272017-04-27 20:43:10 +0100947
Paul Crowleyc6433a22017-10-24 14:54:43 -0700948static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
949 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
950 if (!dirp) {
951 PLOG(ERROR) << "Unable to open directory: " + directory_path;
952 return false;
953 }
954 bool res = true;
955 for (;;) {
956 errno = 0;
957 auto const entry = readdir(dirp.get());
958 if (!entry) {
959 if (errno) {
960 PLOG(ERROR) << "Unable to read directory: " + directory_path;
961 return false;
962 }
963 break;
964 }
965 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
966 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
967 continue;
968 }
969 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
970 }
971 return res;
972}
973
Eric Biggersa701c452018-10-23 13:06:55 -0700974bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700975 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700976 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700977 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
978 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700979 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
980 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
981 return res;
982}