blob: bea9328916f77f3f9c5795f71598b2ff263ef830 [file] [log] [blame]
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Biggersa701c452018-10-23 13:06:55 -070017#include "FsCrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000018
Paul Crowley1ef25582016-01-21 20:26:12 +000019#include "KeyStorage.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070020#include "KeyUtil.h"
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080021#include "Utils.h"
Paul Crowleya7ca40b2017-10-06 14:29:33 -070022#include "VoldUtil.h"
23
Paul Crowleya3630362016-05-17 14:17:56 -070024#include <algorithm>
Paul Lawrence731a7a22015-04-28 22:14:15 +000025#include <map>
Paul Crowleyb1f3d242016-01-28 10:09:46 +000026#include <set>
Paul Lawrencefd7db732015-04-10 07:48:51 -070027#include <sstream>
Paul Crowleydf528a72016-03-09 09:31:37 -080028#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070029#include <vector>
Paul Lawrence731a7a22015-04-28 22:14:15 +000030
Paul Crowleydf528a72016-03-09 09:31:37 -080031#include <dirent.h>
32#include <errno.h>
33#include <fcntl.h>
Paul Crowleya3630362016-05-17 14:17:56 -070034#include <limits.h>
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070035#include <selinux/android.h>
Paul Crowleydf528a72016-03-09 09:31:37 -080036#include <sys/mount.h>
37#include <sys/stat.h>
38#include <sys/types.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070039#include <unistd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000040
Paul Crowley480fcd22015-08-24 14:53:28 +010041#include <private/android_filesystem_config.h>
42
Paul Crowley82b41ff2017-10-20 08:17:54 -070043#include "android/os/IVold.h"
44
Paul Lawrence731a7a22015-04-28 22:14:15 +000045#include "cryptfs.h"
46
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070047#define EMULATED_USES_SELINUX 0
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -060048#define MANAGE_MISC_DIRS 0
Jeff Sharkey7a9dd952016-01-12 16:52:16 -070049
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080050#include <cutils/fs.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070051#include <cutils/properties.h>
52
Eric Biggersa701c452018-10-23 13:06:55 -070053#include <fscrypt/fscrypt.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070054#include <keyutils.h>
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080055
Elliott Hughes7e128fb2015-12-04 15:50:53 -080056#include <android-base/file.h>
Elliott Hughes6bf05472015-12-04 17:55:33 -080057#include <android-base/logging.h>
Paul Crowley3aa914d2017-10-09 16:35:51 -070058#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080059#include <android-base/stringprintf.h>
Eric Biggers83a73d72019-09-30 13:06:47 -070060#include <android-base/strings.h>
Jieb6698d52018-11-12 15:26:02 +080061#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000062
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080063using android::base::StringPrintf;
Tom Cherry4c5bde22019-01-29 14:34:01 -080064using android::fs_mgr::GetEntryForMountPoint;
Paul Crowley05720802016-02-08 15:55:41 +000065using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010066using android::vold::KeyBuffer;
Tommy Chiua98464f2019-03-26 14:14:19 +080067using android::vold::writeStringToFile;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080068
Paul Lawrence731a7a22015-04-28 22:14:15 +000069namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000070
Paul Crowley3aa914d2017-10-09 16:35:51 -070071struct PolicyKeyRef {
72 std::string contents_mode;
73 std::string filenames_mode;
Eric Biggers83a73d72019-09-30 13:06:47 -070074 int policy_version;
Paul Crowley3aa914d2017-10-09 16:35:51 -070075 std::string key_raw_ref;
Eric Biggers83a73d72019-09-30 13:06:47 -070076
77 PolicyKeyRef() : policy_version(0) {}
Paul Crowley3aa914d2017-10-09 16:35:51 -070078};
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 Crowleyc8a3ef32019-09-11 15:00:08 -070091bool s_systemwide_keys_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
Eric Biggers83a73d72019-09-30 13:06:47 -0700206// Retrieve the options to use for encryption policies on the /data filesystem.
207static void get_data_file_encryption_options(PolicyKeyRef* key_ref) {
208 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
209 if (entry == nullptr) {
210 return;
211 }
212 key_ref->contents_mode = entry->file_contents_mode;
213 key_ref->filenames_mode = entry->file_names_mode;
214 key_ref->policy_version = entry->file_policy_version;
215}
216
217// Retrieve the version to use for encryption policies on the /data filesystem.
218static int get_data_file_policy_version(void) {
219 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
220 if (entry == nullptr) {
221 return 0;
222 }
223 return entry->file_policy_version;
224}
225
226// Retrieve the options to use for encryption policies on adoptable storage.
227static bool get_volume_file_encryption_options(PolicyKeyRef* key_ref) {
228 key_ref->contents_mode =
229 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
230 key_ref->filenames_mode =
231 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
232 key_ref->policy_version = 1;
233
234 std::string raw_flags = android::base::GetProperty("ro.crypto.volume.flags", "");
235 auto flags = android::base::Split(raw_flags, "+");
236 for (const auto& flag : flags) {
237 if (flag == "v1") {
238 key_ref->policy_version = 1;
239 } else if (flag == "v2") {
240 key_ref->policy_version = 2;
241 } else {
242 LOG(ERROR) << "Unknown flag in ro.crypto.volume.flags: " << flag;
243 return false;
244 }
245 }
246 return true;
247}
248
Eric Biggersf3dc4202019-09-30 13:05:58 -0700249// Install a key for use by encrypted files on the /data filesystem.
250static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700251 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700252}
253
254// Evict a key for use by encrypted files on the /data filesystem.
255static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700256 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700257}
258
Paul Crowleydf528a72016-03-09 09:31:37 -0800259static bool read_and_install_user_ce_key(userid_t user_id,
260 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000261 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100262 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700263 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000264 std::string ce_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700265 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100266 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000267 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000268 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000269 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000270}
271
Paul Crowleydf528a72016-03-09 09:31:37 -0800272static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000273 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000274 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
275 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000276 return false;
277 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000278 return true;
279}
280
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600281static bool destroy_dir(const std::string& dir) {
282 LOG(DEBUG) << "Destroying: " << dir;
283 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
284 PLOG(ERROR) << "Failed to destroy " << dir;
285 return false;
286 }
287 return true;
288}
289
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000290// NB this assumes that there is only one thread listening for crypt commands, because
291// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000292static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100293 KeyBuffer de_key, ce_key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700294 if (!android::vold::randomKey(&de_key)) return false;
295 if (!android::vold::randomKey(&ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000296 if (create_ephemeral) {
297 // If the key should be created as ephemeral, don't store it.
298 s_ephemeral_users.insert(user_id);
299 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700300 auto const directory_path = get_ce_key_directory_path(user_id);
301 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
302 auto const paths = get_ce_key_paths(directory_path);
303 std::string ce_key_path;
304 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700305 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
306 ce_key))
307 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700308 fixate_user_ce_key(directory_path, ce_key_path, paths);
309 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700310 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700311 kEmptyAuthentication, de_key))
312 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100313 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000314 std::string de_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700315 if (!install_data_key(de_key, &de_raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000316 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000317 std::string ce_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700318 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000319 s_ce_keys[user_id] = ce_key;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000320 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000321 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000322 return true;
323}
324
Paul Crowleydf528a72016-03-09 09:31:37 -0800325static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
326 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000327 auto refi = key_map.find(user_id);
328 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700329 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000330 return false;
331 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800332 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000333 return true;
334}
335
Paul Crowley3aa914d2017-10-09 16:35:51 -0700336static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700337 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700338 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
Eric Biggers83a73d72019-09-30 13:06:47 -0700339 key_ref.filenames_mode.c_str(), key_ref.policy_version) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100340}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100341
Paul Crowleydf528a72016-03-09 09:31:37 -0800342static bool is_numeric(const char* name) {
343 for (const char* p = name; *p != '\0'; p++) {
344 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000345 }
346 return true;
347}
348
349static bool load_all_de_keys() {
350 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800351 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000352 if (!dirp) {
353 PLOG(ERROR) << "Unable to read de key directory";
354 return false;
355 }
356 for (;;) {
357 errno = 0;
358 auto entry = readdir(dirp.get());
359 if (!entry) {
360 if (errno) {
361 PLOG(ERROR) << "Unable to read de key directory";
362 return false;
363 }
364 break;
365 }
366 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
367 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
368 continue;
369 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600370 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000371 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000372 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100373 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800374 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000375 std::string raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700376 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000377 s_de_key_raw_refs[user_id] = raw_ref;
378 LOG(DEBUG) << "Installed de key for user " << user_id;
379 }
380 }
Eric Biggersa701c452018-10-23 13:06:55 -0700381 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000382 // correct policy set on them, and that no rogue ones exist.
383 return true;
384}
385
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700386bool fscrypt_initialize_systemwide_keys() {
387 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800388
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700389 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000390 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000391 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800392 }
393
Paul Crowley3aa914d2017-10-09 16:35:51 -0700394 PolicyKeyRef device_ref;
Eric Biggers83a73d72019-09-30 13:06:47 -0700395 get_data_file_encryption_options(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800396
Eric Biggers83a73d72019-09-30 13:06:47 -0700397 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
398 device_key_temp, "", device_ref.policy_version,
399 &device_ref.key_raw_ref))
400 return false;
401
402 std::string options_string =
403 StringPrintf("%s:%s:v%d", device_ref.contents_mode.c_str(),
404 device_ref.filenames_mode.c_str(), device_ref.policy_version);
405 std::string options_filename = std::string("/data") + fscrypt_key_mode;
406 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700407
Eric Biggersa701c452018-10-23 13:06:55 -0700408 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Tommy Chiua98464f2019-03-26 14:14:19 +0800409 if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700410 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800411
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700412 KeyBuffer per_boot_key;
413 if (!android::vold::randomKey(&per_boot_key)) return false;
414 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700415 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700416 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
417 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
418 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
419
Tommy Chiua98464f2019-03-26 14:14:19 +0800420 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700421 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000422 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800423}
424
Eric Biggersa701c452018-10-23 13:06:55 -0700425bool fscrypt_init_user0() {
426 LOG(DEBUG) << "fscrypt_init_user0";
427 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000428 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
429 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
430 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700431 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000432 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000433 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700434 // TODO: switch to loading only DE_0 here once framework makes
435 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000436 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000437 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700438 // We can only safely prepare DE storage here, since CE keys are probably
439 // entangled with user credentials. The framework will always prepare CE
440 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700441 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700442 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000443 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700444 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700445
446 // If this is a non-FBE device that recently left an emulated mode,
447 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700448 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
449 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700450 }
451
Paul Crowley76107cb2016-02-09 10:04:39 +0000452 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000453}
454
Eric Biggersa701c452018-10-23 13:06:55 -0700455bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
456 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
457 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000458 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000459 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000460 // FIXME test for existence of key that is not loaded yet
461 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700462 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800463 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000464 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000465 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800466 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000467 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000468 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000469 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000470 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800471}
472
Eric Biggersce368682019-04-03 15:44:06 -0700473// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700474// in the case where the keys are being put in the session keyring (rather in
475// the newer filesystem-level keyrings), because removing a key from the session
476// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
477// was already set up. So to remove the per-file keys and make the files
478// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700479//
480// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
481// objects systemwide. This is overkill, but it's the best available method
482// currently. Don't use drop_caches mode "3" because that also evicts pagecache
483// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700484static void drop_caches_if_needed() {
485 if (android::vold::isFsKeyringSupported()) {
486 return;
487 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100488 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700489 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100490 PLOG(ERROR) << "Failed to drop caches during key eviction";
491 }
492}
493
Andrew Scull7ec25c72016-10-31 10:28:25 +0000494static bool evict_ce_key(userid_t user_id) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100495 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000496 bool success = true;
497 std::string raw_ref;
498 // If we haven't loaded the CE key, no need to evict it.
499 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700500 success &= evict_data_key(raw_ref);
501 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000502 }
503 s_ce_key_raw_refs.erase(user_id);
504 return success;
505}
506
Eric Biggersa701c452018-10-23 13:06:55 -0700507bool fscrypt_destroy_user_key(userid_t user_id) {
508 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
509 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000510 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000511 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000512 bool success = true;
513 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000514 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700515 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 +0000516 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000517 auto it = s_ephemeral_users.find(user_id);
518 if (it != s_ephemeral_users.end()) {
519 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000520 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700521 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700522 success &= android::vold::destroyKey(path);
523 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700524 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700525 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700526 success &= android::vold::destroyKey(de_key_path);
527 } else {
528 LOG(INFO) << "Not present so not erasing: " << de_key_path;
529 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100530 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000531 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100532}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800533
Paul Crowley76107cb2016-02-09 10:04:39 +0000534static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700535 if (chmod(path.c_str(), 0000) != 0) {
536 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000537 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700538 }
539#if EMULATED_USES_SELINUX
540 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
541 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000542 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700543 }
544#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000545 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700546}
547
Paul Crowley76107cb2016-02-09 10:04:39 +0000548static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700549 if (chmod(path.c_str(), mode) != 0) {
550 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000551 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700552 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700553 }
554#if EMULATED_USES_SELINUX
555 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
556 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000557 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700558 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700559 }
560#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000561 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700562}
563
Paul Crowley3b71fc52017-10-09 10:55:21 -0700564static bool parse_hex(const std::string& hex, std::string* result) {
565 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800566 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000567 return true;
568 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800569 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800570 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000571 return false;
572 }
573 return true;
574}
575
Paul Crowley3aa914d2017-10-09 16:35:51 -0700576static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
577 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
578}
579
Paul Crowley26a53882017-10-26 11:16:39 -0700580static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
581 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
582}
583
Paul Crowley3aa914d2017-10-09 16:35:51 -0700584static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
585 PolicyKeyRef* key_ref) {
Paul Crowley26a53882017-10-26 11:16:39 -0700586 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
587 std::string secdiscardable_hash;
588 if (android::vold::pathExists(secdiscardable_path)) {
589 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
590 return false;
591 } else {
592 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
593 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
594 return false;
595 }
596 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
597 return false;
598 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700599 auto key_path = volkey_path(misc_path, volume_uuid);
600 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
601 PLOG(ERROR) << "Creating directories for: " << key_path;
602 return false;
603 }
Paul Crowley26a53882017-10-26 11:16:39 -0700604 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700605
606 if (!get_volume_file_encryption_options(key_ref)) return false;
607
608 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
609 volume_uuid, key_ref->policy_version,
610 &key_ref->key_raw_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700611}
612
613static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700614 auto path = volkey_path(misc_path, volume_uuid);
615 if (!android::vold::pathExists(path)) return true;
616 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700617}
618
Eric Biggersa701c452018-10-23 13:06:55 -0700619bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700620 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700621 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700622 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700623 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000624 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700625 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800626 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700627 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700628 auto auth =
629 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowley05720802016-02-08 15:55:41 +0000630 auto it = s_ce_keys.find(user_id);
631 if (it == s_ce_keys.end()) {
632 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000633 return false;
Paul Crowley05720802016-02-08 15:55:41 +0000634 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700635 const auto& ce_key = it->second;
Paul Crowleya3630362016-05-17 14:17:56 -0700636 auto const directory_path = get_ce_key_directory_path(user_id);
637 auto const paths = get_ce_key_paths(directory_path);
638 std::string ce_key_path;
639 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700640 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800641 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700642 return true;
643}
644
Eric Biggersa701c452018-10-23 13:06:55 -0700645bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
646 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
647 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700648 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700649 auto const directory_path = get_ce_key_directory_path(user_id);
650 auto const paths = get_ce_key_paths(directory_path);
651 if (paths.empty()) {
652 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
653 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000654 }
Paul Crowleya3630362016-05-17 14:17:56 -0700655 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000656 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000657}
658
Jeff Sharkey47695b22016-02-01 17:02:29 -0700659// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700660bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700661 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700662 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700663 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700664 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000665 if (s_ce_key_raw_refs.count(user_id) != 0) {
666 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000667 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000668 }
669 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800670 if (!parse_hex(token_hex, &token)) return false;
671 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000672 android::vold::KeyAuthentication auth(token, secret);
673 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000674 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000675 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800676 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700677 } else {
678 // When in emulation mode, we just use chmod. However, we also
679 // unlock directories when not in emulation mode, to bring devices
680 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000681 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800682 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700683 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
684 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700685 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000686 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700687 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800688 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000689 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800690}
691
Jeff Sharkey47695b22016-02-01 17:02:29 -0700692// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700693bool fscrypt_lock_user_key(userid_t user_id) {
694 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
695 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000696 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700697 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800698 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000699 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800700 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700701 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
702 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000703 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000704 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800705 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800706 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700707
Paul Crowley76107cb2016-02-09 10:04:39 +0000708 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800709}
710
Paul Crowley82b41ff2017-10-20 08:17:54 -0700711static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
712 userid_t user_id, int flags) {
713 if (0 != android::vold::ForkExecvp(
714 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
715 std::to_string(user_id), std::to_string(flags)})) {
716 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700717 return false;
718 }
719 return true;
720}
721
Eric Biggersa701c452018-10-23 13:06:55 -0700722bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700723 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700724 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800725 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700726
Paul Crowley82b41ff2017-10-20 08:17:54 -0700727 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600728 // DE_sys key
729 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
730 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
731 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600732
733 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700734 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
735 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800736 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700737 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
738
Paul Crowley3b71fc52017-10-09 10:55:21 -0700739 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700740 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600741#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700742 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700743 multiuser_get_uid(user_id, AID_EVERYBODY)))
744 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600745#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700746 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600747
Paul Crowley6b756ce2017-10-05 14:07:09 -0700748 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
749 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800750 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700751 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000752 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000753
Eric Biggersa701c452018-10-23 13:06:55 -0700754 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700755 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700756 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700757 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700758 get_data_file_encryption_options(&de_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700759 if (!ensure_policy(de_ref, system_de_path)) return false;
760 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800761 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700762 } else {
763 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700764 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700765 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700766 }
Paul Crowley285956f2016-01-20 13:12:38 +0000767 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800768
Paul Crowley82b41ff2017-10-20 08:17:54 -0700769 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600770 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700771 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
772 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800773 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600774 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
775 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800776
Paul Crowley3b71fc52017-10-09 10:55:21 -0700777 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700778 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
779 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800780 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700781 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000782 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
783 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700784
Eric Biggersa701c452018-10-23 13:06:55 -0700785 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700786 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700787 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700788 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700789 get_data_file_encryption_options(&ce_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700790 if (!ensure_policy(ce_ref, system_ce_path)) return false;
791 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800792 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700793
Paul Crowley3aa914d2017-10-09 16:35:51 -0700794 } else {
795 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700796 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700797 if (!ensure_policy(ce_ref, media_ce_path)) return false;
798 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700799 }
Paul Crowley1a965262017-10-13 13:49:50 -0700800
801 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700802 // Now that credentials have been installed, we can run restorecon
803 // over these paths
804 // NOTE: these paths need to be kept in sync with libselinux
805 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700806 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700807 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700808 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800809 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700810 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800811
Paul Crowley76107cb2016-02-09 10:04:39 +0000812 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800813}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600814
Eric Biggersa701c452018-10-23 13:06:55 -0700815bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
816 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600817 << ", user " << user_id << ", flags " << flags;
818 bool res = true;
819
Paul Crowley82b41ff2017-10-20 08:17:54 -0700820 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
821
822 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700823 // CE_n key
824 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
825 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800826 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700827 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
828 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
829
830 res &= destroy_dir(media_ce_path);
831 res &= destroy_dir(user_ce_path);
832 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700833 res &= destroy_dir(system_ce_path);
834 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800835 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700836 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700837 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700838 res &= destroy_volkey(misc_ce_path, volume_uuid);
839 }
Paul Crowley8e550662017-10-16 17:01:44 -0700840 }
841 }
842
Paul Crowley82b41ff2017-10-20 08:17:54 -0700843 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600844 // DE_sys key
845 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
846 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
847 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600848
849 // DE_n key
850 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
851 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800852 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600853 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
854
Paul Crowley8e550662017-10-16 17:01:44 -0700855 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700856 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600857 res &= destroy_dir(system_legacy_path);
858#if MANAGE_MISC_DIRS
859 res &= destroy_dir(misc_legacy_path);
860#endif
861 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600862 res &= destroy_dir(system_de_path);
863 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800864 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700865 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700866 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700867 res &= destroy_volkey(misc_de_path, volume_uuid);
868 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600869 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600870 }
871
872 return res;
873}
Rubin Xu2436e272017-04-27 20:43:10 +0100874
Paul Crowleyc6433a22017-10-24 14:54:43 -0700875static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
876 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
877 if (!dirp) {
878 PLOG(ERROR) << "Unable to open directory: " + directory_path;
879 return false;
880 }
881 bool res = true;
882 for (;;) {
883 errno = 0;
884 auto const entry = readdir(dirp.get());
885 if (!entry) {
886 if (errno) {
887 PLOG(ERROR) << "Unable to read directory: " + directory_path;
888 return false;
889 }
890 break;
891 }
892 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
893 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
894 continue;
895 }
896 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
897 }
898 return res;
899}
900
Eric Biggersa701c452018-10-23 13:06:55 -0700901bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700902 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700903 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700904 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
905 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700906 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
907 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
908 return res;
909}