blob: 97f261915aea32b50628d8e5c60947d4561249a4 [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>
Jieb6698d52018-11-12 15:26:02 +080060#include <android-base/unique_fd.h>
Paul Lawrence731a7a22015-04-28 22:14:15 +000061
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080062using android::base::StringPrintf;
Pavel Grafovb350ed02017-07-27 17:34:57 +010063using android::base::WriteStringToFile;
Paul Crowley05720802016-02-08 15:55:41 +000064using android::vold::kEmptyAuthentication;
Pavel Grafove2e2d302017-08-01 17:15:53 +010065using android::vold::KeyBuffer;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -080066
Paul Lawrence731a7a22015-04-28 22:14:15 +000067namespace {
Andrew Scull7ec25c72016-10-31 10:28:25 +000068
Paul Crowley3aa914d2017-10-09 16:35:51 -070069struct PolicyKeyRef {
70 std::string contents_mode;
71 std::string filenames_mode;
72 std::string key_raw_ref;
73};
74
Eric Biggersa701c452018-10-23 13:06:55 -070075const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
Paul Crowleydf528a72016-03-09 09:31:37 -080076const std::string device_key_path = device_key_dir + "/key";
77const std::string device_key_temp = device_key_dir + "/temp";
Paul Lawrence5a06a642016-02-03 13:39:13 -080078
Paul Crowleydf528a72016-03-09 09:31:37 -080079const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
80const std::string user_key_temp = user_key_dir + "/temp";
Paul Crowley82b41ff2017-10-20 08:17:54 -070081const std::string prepare_subdirs_path = "/system/bin/vold_prepare_subdirs";
Paul Crowley285956f2016-01-20 13:12:38 +000082
Paul Crowley26a53882017-10-26 11:16:39 -070083const std::string systemwide_volume_key_dir =
84 std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
85
Paul Crowleydf528a72016-03-09 09:31:37 -080086bool s_global_de_initialized = false;
Paul Lawrence7b6b5652016-02-02 11:14:59 -080087
Paul Crowleydf528a72016-03-09 09:31:37 -080088// Some users are ephemeral, don't try to wipe their keys from disk
89std::set<userid_t> s_ephemeral_users;
Paul Lawrenceaec34df2016-02-03 10:52:41 -080090
Paul Crowleydf528a72016-03-09 09:31:37 -080091// Map user ids to key references
92std::map<userid_t, std::string> s_de_key_raw_refs;
93std::map<userid_t, std::string> s_ce_key_raw_refs;
Paul Crowley99360d72016-10-19 14:00:24 -070094// TODO abolish this map, per b/26948053
Pavel Grafove2e2d302017-08-01 17:15:53 +010095std::map<userid_t, KeyBuffer> s_ce_keys;
Paul Lawrence731a7a22015-04-28 22:14:15 +000096
Paul Crowley14c8c072018-09-18 13:30:21 -070097} // namespace
Paul Lawrence731a7a22015-04-28 22:14:15 +000098
Eric Biggersa701c452018-10-23 13:06:55 -070099static bool fscrypt_is_emulated() {
Paul Crowley38132a12016-02-09 09:50:32 +0000100 return property_get_bool("persist.sys.emulate_fbe", false);
101}
102
Paul Crowley3b71fc52017-10-09 10:55:21 -0700103static const char* escape_empty(const std::string& value) {
104 return value.empty() ? "null" : value.c_str();
Paul Lawrence731a7a22015-04-28 22:14:15 +0000105}
106
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000107static std::string get_de_key_path(userid_t user_id) {
108 return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
109}
110
Paul Crowleya3630362016-05-17 14:17:56 -0700111static std::string get_ce_key_directory_path(userid_t user_id) {
112 return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id);
113}
114
115// Returns the keys newest first
116static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) {
117 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
118 if (!dirp) {
119 PLOG(ERROR) << "Unable to open ce key directory: " + directory_path;
120 return std::vector<std::string>();
121 }
122 std::vector<std::string> result;
123 for (;;) {
124 errno = 0;
125 auto const entry = readdir(dirp.get());
126 if (!entry) {
127 if (errno) {
128 PLOG(ERROR) << "Unable to read ce key directory: " + directory_path;
129 return std::vector<std::string>();
130 }
131 break;
132 }
133 if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') {
134 LOG(DEBUG) << "Skipping non-key " << entry->d_name;
135 continue;
136 }
137 result.emplace_back(directory_path + "/" + entry->d_name);
138 }
139 std::sort(result.begin(), result.end());
140 std::reverse(result.begin(), result.end());
141 return result;
142}
143
144static std::string get_ce_key_current_path(const std::string& directory_path) {
145 return directory_path + "/current";
146}
147
148static bool get_ce_key_new_path(const std::string& directory_path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700149 const std::vector<std::string>& paths, std::string* ce_key_path) {
Paul Crowleya3630362016-05-17 14:17:56 -0700150 if (paths.empty()) {
151 *ce_key_path = get_ce_key_current_path(directory_path);
152 return true;
153 }
154 for (unsigned int i = 0; i < UINT_MAX; i++) {
155 auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i);
156 if (paths[0] < candidate) {
157 *ce_key_path = candidate;
158 return true;
159 }
160 }
161 return false;
162}
163
164// Discard all keys but the named one; rename it to canonical name.
165// No point in acting on errors in this; ignore them.
Paul Crowley14c8c072018-09-18 13:30:21 -0700166static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix,
Paul Crowleya3630362016-05-17 14:17:56 -0700167 const std::vector<std::string>& paths) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 for (auto const other_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700169 if (other_path != to_fix) {
170 android::vold::destroyKey(other_path);
171 }
172 }
173 auto const current_path = get_ce_key_current_path(directory_path);
174 if (to_fix != current_path) {
175 LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path;
176 if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
177 PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
Jieb6698d52018-11-12 15:26:02 +0800178 return;
Paul Crowleya3630362016-05-17 14:17:56 -0700179 }
180 }
Paul Crowley621d9b92018-12-07 15:36:09 -0800181 android::vold::FsyncDirectory(directory_path);
Paul Crowleya3630362016-05-17 14:17:56 -0700182}
183
184static bool read_and_fixate_user_ce_key(userid_t user_id,
185 const android::vold::KeyAuthentication& auth,
Paul Crowley14c8c072018-09-18 13:30:21 -0700186 KeyBuffer* ce_key) {
Paul Crowleya3630362016-05-17 14:17:56 -0700187 auto const directory_path = get_ce_key_directory_path(user_id);
188 auto const paths = get_ce_key_paths(directory_path);
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 for (auto const ce_key_path : paths) {
Paul Crowleya3630362016-05-17 14:17:56 -0700190 LOG(DEBUG) << "Trying user CE key " << ce_key_path;
191 if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) {
192 LOG(DEBUG) << "Successfully retrieved key";
193 fixate_user_ce_key(directory_path, ce_key_path, paths);
194 return true;
195 }
196 }
197 LOG(ERROR) << "Failed to find working ce key for user " << user_id;
198 return false;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100199}
200
Paul Crowleydf528a72016-03-09 09:31:37 -0800201static bool read_and_install_user_ce_key(userid_t user_id,
202 const android::vold::KeyAuthentication& auth) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000203 if (s_ce_key_raw_refs.count(user_id) != 0) return true;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100204 KeyBuffer ce_key;
Paul Crowleya3630362016-05-17 14:17:56 -0700205 if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000206 std::string ce_raw_ref;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700207 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100208 s_ce_keys[user_id] = std::move(ce_key);
Paul Crowley05720802016-02-08 15:55:41 +0000209 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000210 LOG(DEBUG) << "Installed ce key for user " << user_id;
Paul Crowley1ef25582016-01-21 20:26:12 +0000211 return true;
Paul Crowley285956f2016-01-20 13:12:38 +0000212}
213
Paul Crowleydf528a72016-03-09 09:31:37 -0800214static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000215 LOG(DEBUG) << "Preparing: " << dir;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000216 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
217 PLOG(ERROR) << "Failed to prepare " << dir;
Paul Crowley285956f2016-01-20 13:12:38 +0000218 return false;
219 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000220 return true;
221}
222
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600223static bool destroy_dir(const std::string& dir) {
224 LOG(DEBUG) << "Destroying: " << dir;
225 if (rmdir(dir.c_str()) != 0 && errno != ENOENT) {
226 PLOG(ERROR) << "Failed to destroy " << dir;
227 return false;
228 }
229 return true;
230}
231
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000232// NB this assumes that there is only one thread listening for crypt commands, because
233// it creates keys in a fixed location.
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000234static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100235 KeyBuffer de_key, ce_key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700236 if (!android::vold::randomKey(&de_key)) return false;
237 if (!android::vold::randomKey(&ce_key)) return false;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000238 if (create_ephemeral) {
239 // If the key should be created as ephemeral, don't store it.
240 s_ephemeral_users.insert(user_id);
241 } else {
Paul Crowleya3630362016-05-17 14:17:56 -0700242 auto const directory_path = get_ce_key_directory_path(user_id);
243 if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false;
244 auto const paths = get_ce_key_paths(directory_path);
245 std::string ce_key_path;
246 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700247 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication,
248 ce_key))
249 return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700250 fixate_user_ce_key(directory_path, ce_key_path, paths);
251 // Write DE key second; once this is written, all is good.
Paul Crowleyf71ace32016-06-02 11:01:19 -0700252 if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp,
Paul Crowley14c8c072018-09-18 13:30:21 -0700253 kEmptyAuthentication, de_key))
254 return false;
Paul Crowley95376d62015-05-06 15:04:43 +0100255 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000256 std::string de_raw_ref;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700257 if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000258 s_de_key_raw_refs[user_id] = de_raw_ref;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000259 std::string ce_raw_ref;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700260 if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000261 s_ce_keys[user_id] = ce_key;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000262 s_ce_key_raw_refs[user_id] = ce_raw_ref;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000263 LOG(DEBUG) << "Created keys for user " << user_id;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000264 return true;
265}
266
Paul Crowleydf528a72016-03-09 09:31:37 -0800267static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
268 std::string* raw_ref) {
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000269 auto refi = key_map.find(user_id);
270 if (refi == key_map.end()) {
271 LOG(ERROR) << "Cannot find key for " << user_id;
272 return false;
273 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800274 *raw_ref = refi->second;
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000275 return true;
276}
277
Paul Crowley3aa914d2017-10-09 16:35:51 -0700278static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700279 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700280 char const* contents_mode;
281 char const* filenames_mode;
282 fs_mgr_get_file_encryption_modes(rec, &contents_mode, &filenames_mode);
283 key_ref->contents_mode = contents_mode;
284 key_ref->filenames_mode = filenames_mode;
Paul Crowleya7ca40b2017-10-06 14:29:33 -0700285}
286
Paul Crowley3aa914d2017-10-09 16:35:51 -0700287static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
Eric Biggersa701c452018-10-23 13:06:55 -0700288 return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
Paul Crowley3aa914d2017-10-09 16:35:51 -0700289 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
290 key_ref.filenames_mode.c_str()) == 0;
Paul Crowley95376d62015-05-06 15:04:43 +0100291}
Paul Crowleyb33e8872015-05-19 12:34:09 +0100292
Paul Crowleydf528a72016-03-09 09:31:37 -0800293static bool is_numeric(const char* name) {
294 for (const char* p = name; *p != '\0'; p++) {
295 if (!isdigit(*p)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000296 }
297 return true;
298}
299
300static bool load_all_de_keys() {
301 auto de_dir = user_key_dir + "/de";
Paul Crowleydf528a72016-03-09 09:31:37 -0800302 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000303 if (!dirp) {
304 PLOG(ERROR) << "Unable to read de key directory";
305 return false;
306 }
307 for (;;) {
308 errno = 0;
309 auto entry = readdir(dirp.get());
310 if (!entry) {
311 if (errno) {
312 PLOG(ERROR) << "Unable to read de key directory";
313 return false;
314 }
315 break;
316 }
317 if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
318 LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
319 continue;
320 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600321 userid_t user_id = std::stoi(entry->d_name);
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000322 if (s_de_key_raw_refs.count(user_id) == 0) {
Paul Crowley05720802016-02-08 15:55:41 +0000323 auto key_path = de_dir + "/" + entry->d_name;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100324 KeyBuffer key;
Paul Crowleya051eb72016-03-08 16:08:32 -0800325 if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000326 std::string raw_ref;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700327 if (!android::vold::installKey(key, &raw_ref)) return false;
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000328 s_de_key_raw_refs[user_id] = raw_ref;
329 LOG(DEBUG) << "Installed de key for user " << user_id;
330 }
331 }
Eric Biggersa701c452018-10-23 13:06:55 -0700332 // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000333 // correct policy set on them, and that no rogue ones exist.
334 return true;
335}
336
Eric Biggersa701c452018-10-23 13:06:55 -0700337bool fscrypt_initialize_global_de() {
338 LOG(INFO) << "fscrypt_initialize_global_de";
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800339
Paul Crowley38132a12016-02-09 09:50:32 +0000340 if (s_global_de_initialized) {
341 LOG(INFO) << "Already initialized";
Paul Crowley76107cb2016-02-09 10:04:39 +0000342 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800343 }
344
Paul Crowley3aa914d2017-10-09 16:35:51 -0700345 PolicyKeyRef device_ref;
Paul Crowley26a53882017-10-26 11:16:39 -0700346 if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
347 device_key_temp, &device_ref.key_raw_ref))
Paul Crowley3aa914d2017-10-09 16:35:51 -0700348 return false;
349 get_data_file_encryption_modes(&device_ref);
Eric Biggersb45caaf2017-02-02 14:52:12 -0800350
Paul Crowley3aa914d2017-10-09 16:35:51 -0700351 std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
Eric Biggersa701c452018-10-23 13:06:55 -0700352 std::string mode_filename = std::string("/data") + fscrypt_key_mode;
Eric Biggersb45caaf2017-02-02 14:52:12 -0800353 if (!android::base::WriteStringToFile(modestring, mode_filename)) {
Paul Lawrence6e410592016-05-24 14:20:38 -0700354 PLOG(ERROR) << "Cannot save type";
355 return false;
356 }
357
Eric Biggersa701c452018-10-23 13:06:55 -0700358 std::string ref_filename = std::string("/data") + fscrypt_key_ref;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700359 if (!android::base::WriteStringToFile(device_ref.key_raw_ref, ref_filename)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700360 PLOG(ERROR) << "Cannot save key reference to:" << ref_filename;
Paul Crowley76107cb2016-02-09 10:04:39 +0000361 return false;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800362 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700363 LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800364
Paul Crowley38132a12016-02-09 09:50:32 +0000365 s_global_de_initialized = true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000366 return true;
Paul Lawrenceaec34df2016-02-03 10:52:41 -0800367}
368
Eric Biggersa701c452018-10-23 13:06:55 -0700369bool fscrypt_init_user0() {
370 LOG(DEBUG) << "fscrypt_init_user0";
371 if (fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000372 if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false;
373 if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false;
374 if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700375 if (!android::vold::pathExists(get_de_key_path(0))) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000376 if (!create_and_install_user_keys(0, false)) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000377 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700378 // TODO: switch to loading only DE_0 here once framework makes
379 // explicit calls to install DE keys for secondary users
Paul Crowley76107cb2016-02-09 10:04:39 +0000380 if (!load_all_de_keys()) return false;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000381 }
Jeff Sharkey47695b22016-02-01 17:02:29 -0700382 // We can only safely prepare DE storage here, since CE keys are probably
383 // entangled with user credentials. The framework will always prepare CE
384 // storage once CE keys are installed.
Eric Biggersa701c452018-10-23 13:06:55 -0700385 if (!fscrypt_prepare_user_storage("", 0, 0, android::os::IVold::STORAGE_FLAG_DE)) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700386 LOG(ERROR) << "Failed to prepare user 0 storage";
Paul Crowley76107cb2016-02-09 10:04:39 +0000387 return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700388 }
Jeff Sharkey0754a452016-02-08 12:21:42 -0700389
390 // If this is a non-FBE device that recently left an emulated mode,
391 // restore user data directories to known-good state.
Eric Biggersa701c452018-10-23 13:06:55 -0700392 if (!fscrypt_is_native() && !fscrypt_is_emulated()) {
393 fscrypt_unlock_user_key(0, 0, "!", "!");
Jeff Sharkey0754a452016-02-08 12:21:42 -0700394 }
395
Paul Crowley76107cb2016-02-09 10:04:39 +0000396 return true;
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000397}
398
Eric Biggersa701c452018-10-23 13:06:55 -0700399bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
400 LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial;
401 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000402 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000403 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000404 // FIXME test for existence of key that is not loaded yet
405 if (s_ce_key_raw_refs.count(user_id) != 0) {
Eric Biggersa701c452018-10-23 13:06:55 -0700406 LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
Paul Crowleydf528a72016-03-09 09:31:37 -0800407 << " serial " << serial;
Paul Crowley285956f2016-01-20 13:12:38 +0000408 // FIXME should we fail the command?
Paul Crowley76107cb2016-02-09 10:04:39 +0000409 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800410 }
Paul Crowleyb92f83c2016-02-01 14:10:43 +0000411 if (!create_and_install_user_keys(user_id, ephemeral)) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000412 return false;
Paul Crowley285956f2016-01-20 13:12:38 +0000413 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000414 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800415}
416
Pavel Grafovb350ed02017-07-27 17:34:57 +0100417static void drop_caches() {
418 // Clean any dirty pages (otherwise they won't be dropped).
419 sync();
420 // Drop inode and page caches.
421 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
422 PLOG(ERROR) << "Failed to drop caches during key eviction";
423 }
424}
425
Andrew Scull7ec25c72016-10-31 10:28:25 +0000426static bool evict_ce_key(userid_t user_id) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100427 s_ce_keys.erase(user_id);
Andrew Scull7ec25c72016-10-31 10:28:25 +0000428 bool success = true;
429 std::string raw_ref;
430 // If we haven't loaded the CE key, no need to evict it.
431 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700432 success &= android::vold::evictKey(raw_ref);
Pavel Grafovb350ed02017-07-27 17:34:57 +0100433 drop_caches();
Andrew Scull7ec25c72016-10-31 10:28:25 +0000434 }
435 s_ce_key_raw_refs.erase(user_id);
436 return success;
437}
438
Eric Biggersa701c452018-10-23 13:06:55 -0700439bool fscrypt_destroy_user_key(userid_t user_id) {
440 LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")";
441 if (!fscrypt_is_native()) {
Paul Crowley76107cb2016-02-09 10:04:39 +0000442 return true;
Paul Crowleyea62e262016-01-28 12:23:53 +0000443 }
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000444 bool success = true;
445 std::string raw_ref;
Andrew Scull7ec25c72016-10-31 10:28:25 +0000446 success &= evict_ce_key(user_id);
Paul Crowley14c8c072018-09-18 13:30:21 -0700447 success &=
448 lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
Paul Crowley05720802016-02-08 15:55:41 +0000449 s_de_key_raw_refs.erase(user_id);
Paul Crowleyb1f3d242016-01-28 10:09:46 +0000450 auto it = s_ephemeral_users.find(user_id);
451 if (it != s_ephemeral_users.end()) {
452 s_ephemeral_users.erase(it);
Paul Crowley1ef25582016-01-21 20:26:12 +0000453 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700454 for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) {
Paul Crowleya3630362016-05-17 14:17:56 -0700455 success &= android::vold::destroyKey(path);
456 }
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700457 auto de_key_path = get_de_key_path(user_id);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700458 if (android::vold::pathExists(de_key_path)) {
Paul Crowleyab0b56a2016-07-19 15:29:53 -0700459 success &= android::vold::destroyKey(de_key_path);
460 } else {
461 LOG(INFO) << "Not present so not erasing: " << de_key_path;
462 }
Lenka Trochtova395039f2015-11-25 10:13:03 +0100463 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000464 return success;
Paul Crowleyb33e8872015-05-19 12:34:09 +0100465}
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800466
Paul Crowley76107cb2016-02-09 10:04:39 +0000467static bool emulated_lock(const std::string& path) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700468 if (chmod(path.c_str(), 0000) != 0) {
469 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000470 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700471 }
472#if EMULATED_USES_SELINUX
473 if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
474 PLOG(WARNING) << "Failed to setfilecon " << path;
Paul Crowley76107cb2016-02-09 10:04:39 +0000475 return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700476 }
477#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000478 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700479}
480
Paul Crowley76107cb2016-02-09 10:04:39 +0000481static bool emulated_unlock(const std::string& path, mode_t mode) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700482 if (chmod(path.c_str(), mode) != 0) {
483 PLOG(ERROR) << "Failed to chmod " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000484 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700485 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700486 }
487#if EMULATED_USES_SELINUX
488 if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
489 PLOG(WARNING) << "Failed to restorecon " << path;
Paul Crowleya042cb52016-01-21 17:24:49 +0000490 // FIXME temporary workaround for b/26713622
Eric Biggersa701c452018-10-23 13:06:55 -0700491 if (fscrypt_is_emulated()) return false;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700492 }
493#endif
Paul Crowley76107cb2016-02-09 10:04:39 +0000494 return true;
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700495}
496
Paul Crowley3b71fc52017-10-09 10:55:21 -0700497static bool parse_hex(const std::string& hex, std::string* result) {
498 if (hex == "!") {
Paul Crowleya051eb72016-03-08 16:08:32 -0800499 *result = "";
Paul Crowley05720802016-02-08 15:55:41 +0000500 return true;
501 }
Paul Crowleya051eb72016-03-08 16:08:32 -0800502 if (android::vold::HexToStr(hex, *result) != 0) {
Paul Crowleydf528a72016-03-09 09:31:37 -0800503 LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons
Paul Crowley05720802016-02-08 15:55:41 +0000504 return false;
505 }
506 return true;
507}
508
Paul Crowley3aa914d2017-10-09 16:35:51 -0700509static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) {
510 return misc_path + "/vold/volume_keys/" + volume_uuid + "/default";
511}
512
Paul Crowley26a53882017-10-26 11:16:39 -0700513static std::string volume_secdiscardable_path(const std::string& volume_uuid) {
514 return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable";
515}
516
Paul Crowley3aa914d2017-10-09 16:35:51 -0700517static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
518 PolicyKeyRef* key_ref) {
Paul Crowley26a53882017-10-26 11:16:39 -0700519 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
520 std::string secdiscardable_hash;
521 if (android::vold::pathExists(secdiscardable_path)) {
522 if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash))
523 return false;
524 } else {
525 if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) {
526 PLOG(ERROR) << "Creating directories for: " << secdiscardable_path;
527 return false;
528 }
529 if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash))
530 return false;
531 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700532 auto key_path = volkey_path(misc_path, volume_uuid);
533 if (fs_mkdirs(key_path.c_str(), 0700) != 0) {
534 PLOG(ERROR) << "Creating directories for: " << key_path;
535 return false;
536 }
Paul Crowley26a53882017-10-26 11:16:39 -0700537 android::vold::KeyAuthentication auth("", secdiscardable_hash);
538 if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
Paul Crowley3aa914d2017-10-09 16:35:51 -0700539 &key_ref->key_raw_ref))
540 return false;
541 key_ref->contents_mode =
542 android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
543 key_ref->filenames_mode =
544 android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
545 return true;
546}
547
548static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700549 auto path = volkey_path(misc_path, volume_uuid);
550 if (!android::vold::pathExists(path)) return true;
551 return android::vold::destroyKey(path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700552}
553
Eric Biggersa701c452018-10-23 13:06:55 -0700554bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700555 const std::string& secret_hex) {
Eric Biggersa701c452018-10-23 13:06:55 -0700556 LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial
Paul Crowley3b71fc52017-10-09 10:55:21 -0700557 << " token_present=" << (token_hex != "!");
Eric Biggersa701c452018-10-23 13:06:55 -0700558 if (!fscrypt_is_native()) return true;
Paul Crowley76107cb2016-02-09 10:04:39 +0000559 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700560 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800561 if (!parse_hex(token_hex, &token)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700562 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700563 auto auth =
564 secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret);
Paul Crowley05720802016-02-08 15:55:41 +0000565 auto it = s_ce_keys.find(user_id);
566 if (it == s_ce_keys.end()) {
567 LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000568 return false;
Paul Crowley05720802016-02-08 15:55:41 +0000569 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700570 const auto& ce_key = it->second;
Paul Crowleya3630362016-05-17 14:17:56 -0700571 auto const directory_path = get_ce_key_directory_path(user_id);
572 auto const paths = get_ce_key_paths(directory_path);
573 std::string ce_key_path;
574 if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700575 if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
Paul Crowley621d9b92018-12-07 15:36:09 -0800576 if (!android::vold::FsyncDirectory(directory_path)) return false;
Paul Crowleya3630362016-05-17 14:17:56 -0700577 return true;
578}
579
Eric Biggersa701c452018-10-23 13:06:55 -0700580bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) {
581 LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id;
582 if (!fscrypt_is_native()) return true;
Paul Crowley25a71382016-07-25 15:55:36 -0700583 if (s_ephemeral_users.count(user_id) != 0) return true;
Paul Crowleya3630362016-05-17 14:17:56 -0700584 auto const directory_path = get_ce_key_directory_path(user_id);
585 auto const paths = get_ce_key_paths(directory_path);
586 if (paths.empty()) {
587 LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id;
588 return false;
Paul Crowleyad2eb642016-02-10 17:56:05 +0000589 }
Paul Crowleya3630362016-05-17 14:17:56 -0700590 fixate_user_ce_key(directory_path, paths[0], paths);
Paul Crowley76107cb2016-02-09 10:04:39 +0000591 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000592}
593
Jeff Sharkey47695b22016-02-01 17:02:29 -0700594// TODO: rename to 'install' for consistency, and take flags to know which keys to install
Eric Biggersa701c452018-10-23 13:06:55 -0700595bool fscrypt_unlock_user_key(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_unlock_user_key " << 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()) {
Paul Crowley05720802016-02-08 15:55:41 +0000600 if (s_ce_key_raw_refs.count(user_id) != 0) {
601 LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000602 return true;
Paul Crowley05720802016-02-08 15:55:41 +0000603 }
604 std::string token, secret;
Paul Crowleya051eb72016-03-08 16:08:32 -0800605 if (!parse_hex(token_hex, &token)) return false;
606 if (!parse_hex(secret_hex, &secret)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000607 android::vold::KeyAuthentication auth(token, secret);
608 if (!read_and_install_user_ce_key(user_id, auth)) {
Paul Crowley8fb12fd2016-02-01 14:28:12 +0000609 LOG(ERROR) << "Couldn't read key for " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000610 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800611 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700612 } else {
613 // When in emulation mode, we just use chmod. However, we also
614 // unlock directories when not in emulation mode, to bring devices
615 // back into a known-good state.
Paul Crowley76107cb2016-02-09 10:04:39 +0000616 if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800617 !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700618 !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) ||
619 !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) {
Jeff Sharkey7a9dd952016-01-12 16:52:16 -0700620 LOG(ERROR) << "Failed to unlock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000621 return false;
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700622 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800623 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000624 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800625}
626
Jeff Sharkey47695b22016-02-01 17:02:29 -0700627// TODO: rename to 'evict' for consistency
Eric Biggersa701c452018-10-23 13:06:55 -0700628bool fscrypt_lock_user_key(userid_t user_id) {
629 LOG(DEBUG) << "fscrypt_lock_user_key " << user_id;
630 if (fscrypt_is_native()) {
Andrew Scull7ec25c72016-10-31 10:28:25 +0000631 return evict_ce_key(user_id);
Eric Biggersa701c452018-10-23 13:06:55 -0700632 } else if (fscrypt_is_emulated()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800633 // When in emulation mode, we just use chmod
Paul Crowley76107cb2016-02-09 10:04:39 +0000634 if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
Paul Crowleydf528a72016-03-09 09:31:37 -0800635 !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) ||
Paul Crowley3b71fc52017-10-09 10:55:21 -0700636 !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) ||
637 !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) {
Paul Crowley57eedbf2016-02-09 09:30:23 +0000638 LOG(ERROR) << "Failed to lock user " << user_id;
Paul Crowley76107cb2016-02-09 10:04:39 +0000639 return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800640 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800641 }
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700642
Paul Crowley76107cb2016-02-09 10:04:39 +0000643 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800644}
645
Paul Crowley82b41ff2017-10-20 08:17:54 -0700646static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid,
647 userid_t user_id, int flags) {
648 if (0 != android::vold::ForkExecvp(
649 std::vector<std::string>{prepare_subdirs_path, action, volume_uuid,
650 std::to_string(user_id), std::to_string(flags)})) {
651 LOG(ERROR) << "vold_prepare_subdirs failed";
Paul Crowley8e550662017-10-16 17:01:44 -0700652 return false;
653 }
654 return true;
655}
656
Eric Biggersa701c452018-10-23 13:06:55 -0700657bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial,
Paul Crowley3b71fc52017-10-09 10:55:21 -0700658 int flags) {
Eric Biggersa701c452018-10-23 13:06:55 -0700659 LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid)
Paul Crowleydf528a72016-03-09 09:31:37 -0800660 << ", user " << user_id << ", serial " << serial << ", flags " << flags;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700661
Paul Crowley82b41ff2017-10-20 08:17:54 -0700662 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600663 // DE_sys key
664 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
665 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
666 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600667
668 // DE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700669 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
670 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800671 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700672 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
673
Paul Crowley3b71fc52017-10-09 10:55:21 -0700674 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700675 if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600676#if MANAGE_MISC_DIRS
Paul Crowley6b756ce2017-10-05 14:07:09 -0700677 if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM),
Paul Crowley14c8c072018-09-18 13:30:21 -0700678 multiuser_get_uid(user_id, AID_EVERYBODY)))
679 return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600680#endif
Paul Crowley6b756ce2017-10-05 14:07:09 -0700681 if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600682
Paul Crowley6b756ce2017-10-05 14:07:09 -0700683 if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
684 if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800685 if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700686 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000687 if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Calin Juravled1ee9442016-03-02 18:36:50 +0000688
Eric Biggersa701c452018-10-23 13:06:55 -0700689 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700690 PolicyKeyRef de_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700691 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700692 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
693 get_data_file_encryption_modes(&de_ref);
694 if (!ensure_policy(de_ref, system_de_path)) return false;
695 if (!ensure_policy(de_ref, misc_de_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800696 if (!ensure_policy(de_ref, vendor_de_path)) return false;
Paul Crowley3aa914d2017-10-09 16:35:51 -0700697 } else {
698 if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700699 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700700 if (!ensure_policy(de_ref, user_de_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700701 }
Paul Crowley285956f2016-01-20 13:12:38 +0000702 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800703
Paul Crowley82b41ff2017-10-20 08:17:54 -0700704 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600705 // CE_n key
Jeff Sharkey47695b22016-02-01 17:02:29 -0700706 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
707 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800708 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600709 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
710 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800711
Paul Crowley3b71fc52017-10-09 10:55:21 -0700712 if (volume_uuid.empty()) {
Paul Crowley6b756ce2017-10-05 14:07:09 -0700713 if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
714 if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800715 if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700716 }
Paul Crowley76107cb2016-02-09 10:04:39 +0000717 if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false;
718 if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700719
Eric Biggersa701c452018-10-23 13:06:55 -0700720 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700721 PolicyKeyRef ce_ref;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700722 if (volume_uuid.empty()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700723 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
724 get_data_file_encryption_modes(&ce_ref);
725 if (!ensure_policy(ce_ref, system_ce_path)) return false;
726 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
Andreas Huber71cd43f2018-01-22 11:25:29 -0800727 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700728
Paul Crowley3aa914d2017-10-09 16:35:51 -0700729 } else {
730 if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref)) return false;
Paul Crowley6b756ce2017-10-05 14:07:09 -0700731 }
Paul Crowley3aa914d2017-10-09 16:35:51 -0700732 if (!ensure_policy(ce_ref, media_ce_path)) return false;
733 if (!ensure_policy(ce_ref, user_ce_path)) return false;
Jeff Sharkey47695b22016-02-01 17:02:29 -0700734 }
Paul Crowley1a965262017-10-13 13:49:50 -0700735
736 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700737 // Now that credentials have been installed, we can run restorecon
738 // over these paths
739 // NOTE: these paths need to be kept in sync with libselinux
740 android::vold::RestoreconRecursive(system_ce_path);
741 android::vold::RestoreconRecursive(misc_ce_path);
Paul Crowley1a965262017-10-13 13:49:50 -0700742 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800743 }
Paul Crowley82b41ff2017-10-20 08:17:54 -0700744 if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800745
Paul Crowley76107cb2016-02-09 10:04:39 +0000746 return true;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800747}
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600748
Eric Biggersa701c452018-10-23 13:06:55 -0700749bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) {
750 LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid)
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600751 << ", user " << user_id << ", flags " << flags;
752 bool res = true;
753
Paul Crowley82b41ff2017-10-20 08:17:54 -0700754 res &= prepare_subdirs("destroy", volume_uuid, user_id, flags);
755
756 if (flags & android::os::IVold::STORAGE_FLAG_CE) {
Paul Crowley8e550662017-10-16 17:01:44 -0700757 // CE_n key
758 auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
759 auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800760 auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id);
Paul Crowley8e550662017-10-16 17:01:44 -0700761 auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id);
762 auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id);
763
764 res &= destroy_dir(media_ce_path);
765 res &= destroy_dir(user_ce_path);
766 if (volume_uuid.empty()) {
Paul Crowley8e550662017-10-16 17:01:44 -0700767 res &= destroy_dir(system_ce_path);
768 res &= destroy_dir(misc_ce_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800769 res &= destroy_dir(vendor_ce_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700770 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700771 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700772 res &= destroy_volkey(misc_ce_path, volume_uuid);
773 }
Paul Crowley8e550662017-10-16 17:01:44 -0700774 }
775 }
776
Paul Crowley82b41ff2017-10-20 08:17:54 -0700777 if (flags & android::os::IVold::STORAGE_FLAG_DE) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600778 // DE_sys key
779 auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
780 auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
781 auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600782
783 // DE_n key
784 auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
785 auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800786 auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600787 auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
788
Paul Crowley8e550662017-10-16 17:01:44 -0700789 res &= destroy_dir(user_de_path);
Paul Crowley3b71fc52017-10-09 10:55:21 -0700790 if (volume_uuid.empty()) {
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600791 res &= destroy_dir(system_legacy_path);
792#if MANAGE_MISC_DIRS
793 res &= destroy_dir(misc_legacy_path);
794#endif
795 res &= destroy_dir(profiles_de_path);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600796 res &= destroy_dir(system_de_path);
797 res &= destroy_dir(misc_de_path);
Andreas Huber71cd43f2018-01-22 11:25:29 -0800798 res &= destroy_dir(vendor_de_path);
Paul Crowley3aa914d2017-10-09 16:35:51 -0700799 } else {
Eric Biggersa701c452018-10-23 13:06:55 -0700800 if (fscrypt_is_native()) {
Paul Crowley3aa914d2017-10-09 16:35:51 -0700801 res &= destroy_volkey(misc_de_path, volume_uuid);
802 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600803 }
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600804 }
805
806 return res;
807}
Rubin Xu2436e272017-04-27 20:43:10 +0100808
Paul Crowleyc6433a22017-10-24 14:54:43 -0700809static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) {
810 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir);
811 if (!dirp) {
812 PLOG(ERROR) << "Unable to open directory: " + directory_path;
813 return false;
814 }
815 bool res = true;
816 for (;;) {
817 errno = 0;
818 auto const entry = readdir(dirp.get());
819 if (!entry) {
820 if (errno) {
821 PLOG(ERROR) << "Unable to read directory: " + directory_path;
822 return false;
823 }
824 break;
825 }
826 if (entry->d_type != DT_DIR || entry->d_name[0] == '.') {
827 LOG(DEBUG) << "Skipping non-user " << entry->d_name;
828 continue;
829 }
830 res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid);
831 }
832 return res;
833}
834
Eric Biggersa701c452018-10-23 13:06:55 -0700835bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700836 bool res = true;
Eric Biggersa701c452018-10-23 13:06:55 -0700837 LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid);
Paul Crowley26a53882017-10-26 11:16:39 -0700838 auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
839 res &= android::vold::runSecdiscardSingle(secdiscardable_path);
Paul Crowleyc6433a22017-10-24 14:54:43 -0700840 res &= destroy_volume_keys("/data/misc_ce", volume_uuid);
841 res &= destroy_volume_keys("/data/misc_de", volume_uuid);
842 return res;
843}