blob: c101ba84218c23b9da5801679c1c9931f1477615 [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;
Paul Crowley5e53ff62019-10-24 14:55:17 -070068using namespace android::fscrypt;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080069
Paul Lawrence731a7a22015-04-28 22:14:15 +000070namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000071
Eric Biggersa701c452018-10-23 13:06:55 -070072const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080073const std::string device_key_path = device_key_dir + "/key";
74const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080075
Paul Crowleydf528a72016-03-09 09:31:37 -080076const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
77const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070078const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000079
Paul Crowley26a53882017-10-26 11:16:39 -070080const std::string systemwide_volume_key_dir =
81 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
82
Paul Crowleyc8a3ef32019-09-11 15:00:08 -070083bool s_systemwide_keys_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080084
Paul Crowleydf528a72016-03-09 09:31:37 -080085// Some users are ephemeral, don't try to wipe their keys from disk
86std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080087
Paul Crowleydf528a72016-03-09 09:31:37 -080088// Map user ids to key references
89std::map<userid_t, std::string> s_de_key_raw_refs;
90std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070091// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +010092std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +000093
Paul Crowley14c8c072018-09-18 13:30:21 -070094} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000095
Eric Biggersa701c452018-10-23 13:06:55 -070096static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +000097 return property_get_bool("persist.sys.emulate_fbe", false);
98}
99
Paul Crowley3b71fc52017-10-09 10:55:21 -0700100static const char* escape_empty(const std::string& value) {
101 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000102}
103
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000104static std::string get_de_key_path(userid_t user_id) {
105 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
106}
107
Paul Crowleya3630362016-05-17 14:17:56 -0700108static std::string get_ce_key_directory_path(userid_t user_id) {
109 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
110}
111
112// Returns the keys newest first
113static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
114 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
115 if (!dirp) {
116 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
117 return std::vector<std::string>();
118 }
119 std::vector<std::string> result;
120 for (;;) {
121 errno = 0;
122 auto const entry = readdir(dirp.get());
123 if (!entry) {
124 if (errno) {
125 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
126 return std::vector<std::string>();
127 }
128 break;
129 }
130 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
131 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
132 continue;
133 }
134 result.emplace_back(directory_path + "/" + entry->d_name);
135 }
136 std::sort(result.begin(), result.end());
137 std::reverse(result.begin(), result.end());
138 return result;
139}
140
141static std::string get_ce_key_current_path(const std::string& directory_path) {
142 return directory_path + "/current";
143}
144
145static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700146 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700147 if (paths.empty()) {
148 *ce_key_path = get_ce_key_current_path(directory_path);
149 return true;
150 }
151 for (unsigned int i = 0; i < UINT_MAX; i++) {
152 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
153 if (paths[0] < candidate) {
154 *ce_key_path = candidate;
155 return true;
156 }
157 }
158 return false;
159}
160
161// Discard all keys but the named one; rename it to canonical name.
162// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700163static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700164 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700165 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700166 if (other_path != to_fix) {
167 android::vold::destroyKey(other_path);
168 }
169 }
170 auto const current_path = get_ce_key_current_path(directory_path);
171 if (to_fix != current_path) {
172 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
173 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
174 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800175 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700176 }
177 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800178 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700179}
180
181static bool read_and_fixate_user_ce_key(userid_t user_id,
182 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700183 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700184 auto const directory_path = get_ce_key_directory_path(user_id);
185 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700187 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
188 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
189 LOG(DEBUG) << "Successfully retrieved key";
190 fixate_user_ce_key(directory_path, ce_key_path, paths);
191 return true;
192 }
193 }
194 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
195 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100196}
197
Eric Biggers83a73d72019-09-30 13:06:47 -0700198// Retrieve the options to use for encryption policies on the /data filesystem.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700199static void get_data_file_encryption_options(EncryptionOptions* options) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700200 auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
201 if (entry == nullptr) {
202 return;
203 }
Paul Crowleya50f6c32019-10-24 23:21:44 -0700204 if (!ParseOptions(entry->encryption_options, options)) {
205 LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
206 << entry->encryption_options;
207 return;
208 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700209}
210
211// Retrieve the version to use for encryption policies on the /data filesystem.
212static int get_data_file_policy_version(void) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700213 EncryptionOptions options;
214 get_data_file_encryption_options(&options);
215 return options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700216}
217
218// Retrieve the options to use for encryption policies on adoptable storage.
Paul Crowley5e53ff62019-10-24 14:55:17 -0700219static bool get_volume_file_encryption_options(EncryptionOptions* options) {
Paul Crowleyf612b8b2019-10-24 22:52:02 -0700220 auto contents_mode =
221 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
222 auto filenames_mode =
223 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
224 return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
225 contents_mode + ":" + filenames_mode + ":v1"),
226 options);
Eric Biggers83a73d72019-09-30 13:06:47 -0700227}
228
Eric Biggersf3dc4202019-09-30 13:05:58 -0700229// Install a key for use by encrypted files on the /data filesystem.
230static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700231 return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700232}
233
234// Evict a key for use by encrypted files on the /data filesystem.
235static bool evict_data_key(const std::string& raw_ref) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700236 return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
Eric Biggersf3dc4202019-09-30 13:05:58 -0700237}
238
Paul Crowleydf528a72016-03-09 09:31:37 -0800239static bool read_and_install_user_ce_key(userid_t user_id,
240 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000241 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100242 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700243 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000244 std::string ce_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700245 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100246 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000247 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000248 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000249 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000250}
251
Paul Crowleydf528a72016-03-09 09:31:37 -0800252static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000253 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000254 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
255 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000256 return false;
257 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000258 return true;
259}
260
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600261static bool destroy_dir(const std::string& dir) {
262 LOG(DEBUG) << "Destroying: " << dir;
263 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
264 PLOG(ERROR) << "Failed to destroy " << dir;
265 return false;
266 }
267 return true;
268}
269
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000270// NB this assumes that there is only one thread listening for crypt commands, because
271// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000272static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100273 KeyBuffer de_key, ce_key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700274 if (!android::vold::randomKey(&de_key)) return false;
275 if (!android::vold::randomKey(&ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000276 if (create_ephemeral) {
277 // If the key should be created as ephemeral, don't store it.
278 s_ephemeral_users.insert(user_id);
279 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700280 auto const directory_path = get_ce_key_directory_path(user_id);
281 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
282 auto const paths = get_ce_key_paths(directory_path);
283 std::string ce_key_path;
284 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700285 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
286 ce_key))
287 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700288 fixate_user_ce_key(directory_path, ce_key_path, paths);
289 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700290 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700291 kEmptyAuthentication, de_key))
292 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100293 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000294 std::string de_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700295 if (!install_data_key(de_key, &de_raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000296 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000297 std::string ce_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700298 if (!install_data_key(ce_key, &ce_raw_ref)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000299 s_ce_keys[user_id] = ce_key;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000300 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000301 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000302 return true;
303}
304
Paul Crowleydf528a72016-03-09 09:31:37 -0800305static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
306 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000307 auto refi = key_map.find(user_id);
308 if (refi == key_map.end()) {
Eric Biggersd1034042019-04-02 10:38:15 -0700309 LOG(DEBUG) << "Cannot find key for " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000310 return false;
311 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800312 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000313 return true;
314}
315
Paul Crowleydf528a72016-03-09 09:31:37 -0800316static bool is_numeric(const char* name) {
317 for (const char* p = name; *p != '\0'; p++) {
318 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000319 }
320 return true;
321}
322
323static bool load_all_de_keys() {
324 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800325 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000326 if (!dirp) {
327 PLOG(ERROR) << "Unable to read de key directory";
328 return false;
329 }
330 for (;;) {
331 errno = 0;
332 auto entry = readdir(dirp.get());
333 if (!entry) {
334 if (errno) {
335 PLOG(ERROR) << "Unable to read de key directory";
336 return false;
337 }
338 break;
339 }
340 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
341 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
342 continue;
343 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600344 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000345 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000346 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100347 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800348 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000349 std::string raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700350 if (!install_data_key(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000351 s_de_key_raw_refs[user_id] = raw_ref;
352 LOG(DEBUG) << "Installed de key for user " << user_id;
353 }
354 }
Eric Biggersa701c452018-10-23 13:06:55 -0700355 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000356 // correct policy set on them, and that no rogue ones exist.
357 return true;
358}
359
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700360bool fscrypt_initialize_systemwide_keys() {
361 LOG(INFO) << "fscrypt_initialize_systemwide_keys";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800362
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700363 if (s_systemwide_keys_initialized) {
Paul Crowley38132a12016-02-09 09:50:32 +0000364 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000365 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800366 }
367
Paul Crowley5e53ff62019-10-24 14:55:17 -0700368 EncryptionPolicy device_policy;
369 get_data_file_encryption_options(&device_policy.options);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800370
Eric Biggers83a73d72019-09-30 13:06:47 -0700371 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700372 device_key_temp, "", device_policy.options.version,
373 &device_policy.key_raw_ref))
Eric Biggers83a73d72019-09-30 13:06:47 -0700374 return false;
375
Paul Crowley5e53ff62019-10-24 14:55:17 -0700376 std::string options_string;
377 if (!OptionsToString(device_policy.options, &options_string)) {
378 LOG(ERROR) << "Unable to serialize options";
379 return false;
380 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700381 std::string options_filename = std::string("/data") + fscrypt_key_mode;
382 if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
Paul Lawrence6e410592016-05-24 14:20:38 -0700383
Eric Biggersa701c452018-10-23 13:06:55 -0700384 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley5e53ff62019-10-24 14:55:17 -0700385 if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700386 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800387
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700388 KeyBuffer per_boot_key;
389 if (!android::vold::randomKey(&per_boot_key)) return false;
390 std::string per_boot_raw_ref;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700391 if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700392 std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
393 if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
394 LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
395
Tommy Chiua98464f2019-03-26 14:14:19 +0800396 if (!android::vold::FsyncDirectory(device_key_dir)) return false;
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700397 s_systemwide_keys_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000398 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800399}
400
Eric Biggersa701c452018-10-23 13:06:55 -0700401bool fscrypt_init_user0() {
402 LOG(DEBUG) << "fscrypt_init_user0";
403 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000404 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
405 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
406 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700407 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000408 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000409 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700410 // TODO: switch to loading only DE_0 here once framework makes
411 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000412 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000413 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700414 // We can only safely prepare DE storage here, since CE keys are probably
415 // entangled with user credentials. The framework will always prepare CE
416 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700417 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700418 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000419 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700420 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700421
422 // If this is a non-FBE device that recently left an emulated mode,
423 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700424 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
425 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700426 }
427
Paul Crowley76107cb2016-02-09 10:04:39 +0000428 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000429}
430
Eric Biggersa701c452018-10-23 13:06:55 -0700431bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
432 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
433 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000434 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000435 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000436 // FIXME test for existence of key that is not loaded yet
437 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700438 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800439 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000440 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000441 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800442 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000443 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000444 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000445 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000446 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800447}
448
Eric Biggersce368682019-04-03 15:44:06 -0700449// "Lock" all encrypted directories whose key has been removed. This is needed
Eric Biggersf3dc4202019-09-30 13:05:58 -0700450// in the case where the keys are being put in the session keyring (rather in
451// the newer filesystem-level keyrings), because removing a key from the session
452// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
453// was already set up. So to remove the per-file keys and make the files
454// "appear encrypted", these inodes must be evicted.
Eric Biggersce368682019-04-03 15:44:06 -0700455//
456// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
457// objects systemwide. This is overkill, but it's the best available method
458// currently. Don't use drop_caches mode "3" because that also evicts pagecache
459// for in-use files; all files relevant here are already closed and sync'ed.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700460static void drop_caches_if_needed() {
461 if (android::vold::isFsKeyringSupported()) {
462 return;
463 }
Pavel Grafovb350ed02017-07-27 17:34:57 +0100464 sync();
Eric Biggersce368682019-04-03 15:44:06 -0700465 if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
Pavel Grafovb350ed02017-07-27 17:34:57 +0100466 PLOG(ERROR) << "Failed to drop caches during key eviction";
467 }
468}
469
Andrew Scull7ec25c72016-10-31 10:28:25 +0000470static bool evict_ce_key(userid_t user_id) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100471 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000472 bool success = true;
473 std::string raw_ref;
474 // If we haven't loaded the CE key, no need to evict it.
475 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700476 success &= evict_data_key(raw_ref);
477 drop_caches_if_needed();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000478 }
479 s_ce_key_raw_refs.erase(user_id);
480 return success;
481}
482
Eric Biggersa701c452018-10-23 13:06:55 -0700483bool fscrypt_destroy_user_key(userid_t user_id) {
484 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
485 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000486 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000487 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000488 bool success = true;
489 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000490 success &= evict_ce_key(user_id);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700491 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 +0000492 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000493 auto it = s_ephemeral_users.find(user_id);
494 if (it != s_ephemeral_users.end()) {
495 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000496 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700497 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700498 success &= android::vold::destroyKey(path);
499 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700500 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700501 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700502 success &= android::vold::destroyKey(de_key_path);
503 } else {
504 LOG(INFO) << "Not present so not erasing: " << de_key_path;
505 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100506 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000507 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100508}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800509
Paul Crowley76107cb2016-02-09 10:04:39 +0000510static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700511 if (chmod(path.c_str(), 0000) != 0) {
512 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000513 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700514 }
515#if EMULATED_USES_SELINUX
516 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
517 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000518 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700519 }
520#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000521 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700522}
523
Paul Crowley76107cb2016-02-09 10:04:39 +0000524static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700525 if (chmod(path.c_str(), mode) != 0) {
526 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000527 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700528 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700529 }
530#if EMULATED_USES_SELINUX
531 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
532 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000533 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700534 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700535 }
536#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000537 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700538}
539
Paul Crowley3b71fc52017-10-09 10:55:21 -0700540static bool parse_hex(const std::string& hex, std::string* result) {
541 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800542 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000543 return true;
544 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800545 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800546 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000547 return false;
548 }
549 return true;
550}
551
Paul Crowley3aa914d2017-10-09 16:35:51 -0700552static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
553 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
554}
555
Paul Crowley26a53882017-10-26 11:16:39 -0700556static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
557 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
558}
559
Paul Crowley3aa914d2017-10-09 16:35:51 -0700560static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
Paul Crowley5e53ff62019-10-24 14:55:17 -0700561 EncryptionPolicy* policy) {
Paul Crowley26a53882017-10-26 11:16:39 -0700562 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
563 std::string secdiscardable_hash;
564 if (android::vold::pathExists(secdiscardable_path)) {
565 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
566 return false;
567 } else {
568 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
569 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
570 return false;
571 }
572 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
573 return false;
574 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700575 auto key_path = volkey_path(misc_path, volume_uuid);
576 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
577 PLOG(ERROR) << "Creating directories for: " << key_path;
578 return false;
579 }
Paul Crowley26a53882017-10-26 11:16:39 -0700580 android::vold::KeyAuthentication auth("", secdiscardable_hash);
Eric Biggers83a73d72019-09-30 13:06:47 -0700581
Paul Crowley5e53ff62019-10-24 14:55:17 -0700582 if (!get_volume_file_encryption_options(&policy->options)) return false;
Eric Biggers83a73d72019-09-30 13:06:47 -0700583
584 return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley5e53ff62019-10-24 14:55:17 -0700585 volume_uuid, policy->options.version,
586 &policy->key_raw_ref);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700587}
588
589static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700590 auto path = volkey_path(misc_path, volume_uuid);
591 if (!android::vold::pathExists(path)) return true;
592 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700593}
594
Eric Biggersa701c452018-10-23 13:06:55 -0700595bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700596 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700597 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700598 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700599 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000600 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700601 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800602 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700603 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700604 auto auth =
605 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowley05720802016-02-08 15:55:41 +0000606 auto it = s_ce_keys.find(user_id);
607 if (it == s_ce_keys.end()) {
608 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000609 return false;
Paul Crowley05720802016-02-08 15:55:41 +0000610 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700611 const auto& ce_key = it->second;
Paul Crowleya3630362016-05-17 14:17:56 -0700612 auto const directory_path = get_ce_key_directory_path(user_id);
613 auto const paths = get_ce_key_paths(directory_path);
614 std::string ce_key_path;
615 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700616 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800617 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700618 return true;
619}
620
Eric Biggersa701c452018-10-23 13:06:55 -0700621bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
622 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
623 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700624 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700625 auto const directory_path = get_ce_key_directory_path(user_id);
626 auto const paths = get_ce_key_paths(directory_path);
627 if (paths.empty()) {
628 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
629 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000630 }
Paul Crowleya3630362016-05-17 14:17:56 -0700631 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000632 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000633}
634
Jeff Sharkey47695b22016-02-01 17:02:29 -0700635// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700636bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700637 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700638 LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700639 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700640 if (fscrypt_is_native()) {
Paul Crowley05720802016-02-08 15:55:41 +0000641 if (s_ce_key_raw_refs.count(user_id) != 0) {
642 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000643 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000644 }
645 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800646 if (!parse_hex(token_hex, &token)) return false;
647 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000648 android::vold::KeyAuthentication auth(token, secret);
649 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000650 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000651 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800652 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700653 } else {
654 // When in emulation mode, we just use chmod. However, we also
655 // unlock directories when not in emulation mode, to bring devices
656 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000657 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800658 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700659 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
660 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700661 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000662 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700663 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800664 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000665 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800666}
667
Jeff Sharkey47695b22016-02-01 17:02:29 -0700668// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700669bool fscrypt_lock_user_key(userid_t user_id) {
670 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
671 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000672 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700673 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800674 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000675 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800676 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700677 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
678 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000679 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000680 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800681 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800682 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700683
Paul Crowley76107cb2016-02-09 10:04:39 +0000684 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800685}
686
Paul Crowley82b41ff2017-10-20 08:17:54 -0700687static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
688 userid_t user_id, int flags) {
689 if (0 != android::vold::ForkExecvp(
690 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
691 std::to_string(user_id), std::to_string(flags)})) {
692 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700693 return false;
694 }
695 return true;
696}
697
Eric Biggersa701c452018-10-23 13:06:55 -0700698bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700699 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700700 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800701 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700702
Paul Crowley82b41ff2017-10-20 08:17:54 -0700703 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600704 // DE_sys key
705 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
706 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
707 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600708
709 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700710 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
711 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800712 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700713 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
714
Paul Crowley3b71fc52017-10-09 10:55:21 -0700715 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700716 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600717#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700718 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700719 multiuser_get_uid(user_id, AID_EVERYBODY)))
720 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600721#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700722 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600723
Paul Crowley6b756ce2017-10-05 14:07:09 -0700724 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
725 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800726 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700727 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000728 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000729
Eric Biggersa701c452018-10-23 13:06:55 -0700730 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700731 EncryptionPolicy de_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700732 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700733 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
734 return false;
735 get_data_file_encryption_options(&de_policy.options);
736 if (!EnsurePolicy(de_policy, system_de_path)) return false;
737 if (!EnsurePolicy(de_policy, misc_de_path)) return false;
738 if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700739 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700740 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700741 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700742 if (!EnsurePolicy(de_policy, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700743 }
Paul Crowley285956f2016-01-20 13:12:38 +0000744 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800745
Paul Crowley82b41ff2017-10-20 08:17:54 -0700746 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600747 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700748 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
749 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800750 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600751 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
752 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800753
Paul Crowley3b71fc52017-10-09 10:55:21 -0700754 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700755 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
756 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800757 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700758 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000759 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
760 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700761
Eric Biggersa701c452018-10-23 13:06:55 -0700762 if (fscrypt_is_native()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700763 EncryptionPolicy ce_policy;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700764 if (volume_uuid.empty()) {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700765 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
766 return false;
767 get_data_file_encryption_options(&ce_policy.options);
768 if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
769 if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
770 if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700771
Paul Crowley3aa914d2017-10-09 16:35:51 -0700772 } else {
Paul Crowley5e53ff62019-10-24 14:55:17 -0700773 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700774 }
Paul Crowley5e53ff62019-10-24 14:55:17 -0700775 if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
776 if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700777 }
Paul Crowley1a965262017-10-13 13:49:50 -0700778
779 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700780 // Now that credentials have been installed, we can run restorecon
781 // over these paths
782 // NOTE: these paths need to be kept in sync with libselinux
783 android::vold::RestoreconRecursive(system_ce_path);
Nick Kralevich6a3ef482019-05-14 09:30:29 -0700784 android::vold::RestoreconRecursive(vendor_ce_path);
Paul Crowley8e550662017-10-16 17:01:44 -0700785 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700786 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800787 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700788 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800789
Paul Crowley76107cb2016-02-09 10:04:39 +0000790 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800791}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600792
Eric Biggersa701c452018-10-23 13:06:55 -0700793bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
794 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600795 << ", user " << user_id << ", flags " << flags;
796 bool res = true;
797
Paul Crowley82b41ff2017-10-20 08:17:54 -0700798 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
799
800 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700801 // CE_n key
802 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
803 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800804 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700805 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
806 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
807
808 res &= destroy_dir(media_ce_path);
809 res &= destroy_dir(user_ce_path);
810 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700811 res &= destroy_dir(system_ce_path);
812 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800813 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700814 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700815 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700816 res &= destroy_volkey(misc_ce_path, volume_uuid);
817 }
Paul Crowley8e550662017-10-16 17:01:44 -0700818 }
819 }
820
Paul Crowley82b41ff2017-10-20 08:17:54 -0700821 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600822 // DE_sys key
823 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
824 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
825 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600826
827 // DE_n key
828 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
829 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800830 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600831 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
832
Paul Crowley8e550662017-10-16 17:01:44 -0700833 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700834 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600835 res &= destroy_dir(system_legacy_path);
836#if MANAGE_MISC_DIRS
837 res &= destroy_dir(misc_legacy_path);
838#endif
839 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600840 res &= destroy_dir(system_de_path);
841 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800842 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700843 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700844 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700845 res &= destroy_volkey(misc_de_path, volume_uuid);
846 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600847 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600848 }
849
850 return res;
851}
Rubin Xu2436e272017-04-27 20:43:10 +0100852
Paul Crowleyc6433a22017-10-24 14:54:43 -0700853static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
854 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
855 if (!dirp) {
856 PLOG(ERROR) << "Unable to open directory: " + directory_path;
857 return false;
858 }
859 bool res = true;
860 for (;;) {
861 errno = 0;
862 auto const entry = readdir(dirp.get());
863 if (!entry) {
864 if (errno) {
865 PLOG(ERROR) << "Unable to read directory: " + directory_path;
866 return false;
867 }
868 break;
869 }
870 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
871 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
872 continue;
873 }
874 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
875 }
876 return res;
877}
878
Eric Biggersa701c452018-10-23 13:06:55 -0700879bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700880 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700881 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700882 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
883 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700884 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
885 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
886 return res;
887}