bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "FsCrypt.h" |
| 18 | |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 19 | #include "Keymaster.h" |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 20 | #include "KeyStorage.h" |
| 21 | #include "KeyUtil.h" |
| 22 | #include "Utils.h" |
| 23 | // #include "VoldUtil.h" |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <map> |
| 27 | #include <set> |
| 28 | #include <sstream> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <dirent.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <limits.h> |
| 36 | #include <selinux/android.h> |
| 37 | #include <sys/mount.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | #include <private/android_filesystem_config.h> |
| 43 | |
| 44 | // #include "android/os/IVold.h" |
| 45 | |
| 46 | #include "cryptfs.h" |
| 47 | |
| 48 | #define EMULATED_USES_SELINUX 0 |
| 49 | #define MANAGE_MISC_DIRS 0 |
| 50 | |
| 51 | #include <cutils/fs.h> |
| 52 | #include <cutils/properties.h> |
| 53 | |
| 54 | #include <fscrypt/fscrypt.h> |
| 55 | #include <fs_mgr.h> |
| 56 | #include <keyutils.h> |
| 57 | |
| 58 | #include <android-base/file.h> |
| 59 | #include <android-base/logging.h> |
| 60 | #include <android-base/properties.h> |
| 61 | #include <android-base/stringprintf.h> |
| 62 | #include <android-base/unique_fd.h> |
| 63 | |
| 64 | |
| 65 | using android::base::StringPrintf; |
| 66 | using android::fs_mgr::GetEntryForMountPoint; |
| 67 | using android::vold::kEmptyAuthentication; |
| 68 | using android::vold::KeyBuffer; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 69 | using android::vold::Keymaster; |
| 70 | using android::hardware::keymaster::V4_0::KeyFormat; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 71 | // using android::vold::writeStringToFile; |
| 72 | |
| 73 | // Store main DE raw ref / policy |
| 74 | std::string de_raw_ref; |
| 75 | std::map<userid_t, std::string> s_de_key_raw_refs; |
| 76 | std::map<userid_t, std::string> s_ce_key_raw_refs; |
| 77 | |
| 78 | namespace { |
| 79 | |
| 80 | struct PolicyKeyRef { |
| 81 | std::string contents_mode; |
| 82 | std::string filenames_mode; |
| 83 | std::string key_raw_ref; |
| 84 | }; |
| 85 | |
| 86 | const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder; |
| 87 | const std::string device_key_path = device_key_dir + "/key"; |
| 88 | const std::string device_key_temp = device_key_dir + "/temp"; |
| 89 | |
| 90 | const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys"; |
| 91 | const std::string user_key_temp = user_key_dir + "/temp"; |
| 92 | const std::string prepare_subdirs_path = "/sbin/vold_prepare_subdirs"; |
| 93 | |
| 94 | const std::string systemwide_volume_key_dir = |
| 95 | std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys"; |
| 96 | const int STORAGE_FLAG_DE = 1; |
| 97 | const int STORAGE_FLAG_CE = 2; |
| 98 | |
| 99 | bool s_systemwide_keys_initialized = false; |
| 100 | |
| 101 | android::fs_mgr::Fstab fstab_default; |
| 102 | |
| 103 | // Some users are ephemeral, don't try to wipe their keys from disk |
| 104 | std::set<userid_t> s_ephemeral_users; |
| 105 | |
| 106 | // TODO abolish this map, per b/26948053 |
| 107 | std::map<userid_t, KeyBuffer> s_ce_keys; |
| 108 | |
| 109 | } // namespace |
| 110 | |
| 111 | static bool fscrypt_is_emulated() { |
| 112 | return property_get_bool("persist.sys.emulate_fbe", false); |
| 113 | } |
| 114 | |
| 115 | static const char* escape_empty(const std::string& value) { |
| 116 | return value.empty() ? "null" : value.c_str(); |
| 117 | } |
| 118 | |
| 119 | static std::string get_de_key_path(userid_t user_id) { |
| 120 | return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id); |
| 121 | } |
| 122 | |
| 123 | static std::string get_ce_key_directory_path(userid_t user_id) { |
| 124 | return StringPrintf("%s/ce/%d", user_key_dir.c_str(), user_id); |
| 125 | } |
| 126 | |
| 127 | // Returns the keys newest first |
| 128 | static std::vector<std::string> get_ce_key_paths(const std::string& directory_path) { |
| 129 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir); |
| 130 | if (!dirp) { |
| 131 | PLOG(ERROR) << "Unable to open ce key directory: " + directory_path; |
| 132 | return std::vector<std::string>(); |
| 133 | } |
| 134 | std::vector<std::string> result; |
| 135 | for (;;) { |
| 136 | errno = 0; |
| 137 | auto const entry = readdir(dirp.get()); |
| 138 | if (!entry) { |
| 139 | if (errno) { |
| 140 | PLOG(ERROR) << "Unable to read ce key directory: " + directory_path; |
| 141 | return std::vector<std::string>(); |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | if (entry->d_type != DT_DIR || entry->d_name[0] != 'c') { |
| 146 | LOG(DEBUG) << "Skipping non-key " << entry->d_name; |
| 147 | continue; |
| 148 | } |
| 149 | result.emplace_back(directory_path + "/" + entry->d_name); |
| 150 | } |
| 151 | std::sort(result.begin(), result.end()); |
| 152 | std::reverse(result.begin(), result.end()); |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | static std::string get_ce_key_current_path(const std::string& directory_path) { |
| 157 | return directory_path + "/current"; |
| 158 | } |
| 159 | |
| 160 | static bool get_ce_key_new_path(const std::string& directory_path, |
| 161 | const std::vector<std::string>& paths, std::string* ce_key_path) { |
| 162 | if (paths.empty()) { |
| 163 | *ce_key_path = get_ce_key_current_path(directory_path); |
| 164 | return true; |
| 165 | } |
| 166 | for (unsigned int i = 0; i < UINT_MAX; i++) { |
| 167 | auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i); |
| 168 | if (paths[0] < candidate) { |
| 169 | *ce_key_path = candidate; |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | // Discard all keys but the named one; rename it to canonical name. |
| 177 | // No point in acting on errors in this; ignore them. |
| 178 | static void fixate_user_ce_key(const std::string& directory_path, const std::string& to_fix, |
| 179 | const std::vector<std::string>& paths) { |
| 180 | for (auto const other_path : paths) { |
| 181 | if (other_path != to_fix) { |
| 182 | android::vold::destroyKey(other_path); |
| 183 | } |
| 184 | } |
| 185 | auto const current_path = get_ce_key_current_path(directory_path); |
| 186 | if (to_fix != current_path) { |
| 187 | LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path; |
| 188 | if (rename(to_fix.c_str(), current_path.c_str()) != 0) { |
| 189 | PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path; |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | android::vold::FsyncDirectory(directory_path); |
| 194 | } |
| 195 | |
| 196 | static bool read_and_fixate_user_ce_key(userid_t user_id, |
| 197 | const android::vold::KeyAuthentication& auth, |
| 198 | KeyBuffer* ce_key) { |
| 199 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 200 | auto const paths = get_ce_key_paths(directory_path); |
| 201 | for (auto const ce_key_path : paths) { |
| 202 | LOG(DEBUG) << "Trying user CE key " << ce_key_path; |
| 203 | if (android::vold::retrieveKey(ce_key_path, auth, ce_key)) { |
| 204 | LOG(DEBUG) << "Successfully retrieved key"; |
| 205 | fixate_user_ce_key(directory_path, ce_key_path, paths); |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | LOG(ERROR) << "Failed to find working ce key for user " << user_id; |
| 210 | return false; |
| 211 | } |
| 212 | |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 213 | static bool is_wrapped_key_supported_common(const std::string& mount_point) { |
mauronofrio matarrese | 17fb121 | 2020-05-25 19:59:17 +0200 | [diff] [blame] | 214 | LOG(DEBUG) << "Determining wrapped-key support for " << mount_point; |
| 215 | std::string wrapped_key_supported = android::base::GetProperty("fbe.data.wrappedkey", "false"); |
| 216 | LOG(DEBUG) << "fbe.data.wrappedkey = " << wrapped_key_supported; |
| 217 | if (mount_point == DATA_MNT_POINT && wrapped_key_supported == "true") { |
| 218 | LOG(DEBUG) << "Wrapped key supported on " << mount_point; |
| 219 | return true; |
| 220 | } else { |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 221 | return false; |
| 222 | } |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool is_wrapped_key_supported() { |
| 226 | return is_wrapped_key_supported_common(DATA_MNT_POINT); |
| 227 | } |
| 228 | |
| 229 | bool is_wrapped_key_supported_external() { |
| 230 | return false; |
| 231 | } |
| 232 | |
mauronofrio | 539597d | 2020-05-25 22:23:48 +0200 | [diff] [blame^] | 233 | bool is_metadata_wrapped_key_supported_common(const std::string& mount_point) { |
mauronofrio matarrese | f1079ed | 2020-05-25 20:52:57 +0200 | [diff] [blame] | 234 | LOG(DEBUG) << "Determining metadata wrapped-key support for " << mount_point; |
| 235 | std::string wrapped_key_supported = android::base::GetProperty("fbe.metadata.wrappedkey", "false"); |
| 236 | LOG(DEBUG) << "fbe.metadata.wrappedkey = " << wrapped_key_supported; |
| 237 | if (mount_point == METADATA_MNT_POINT && wrapped_key_supported == "true") { |
| 238 | LOG(DEBUG) << "Wrapped key supported on " << mount_point; |
| 239 | return true; |
| 240 | } else { |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
mauronofrio | 539597d | 2020-05-25 22:23:48 +0200 | [diff] [blame^] | 245 | bool is_metadata_wrapped_key_supported() { |
| 246 | return is_metadata_wrapped_key_supported_common(METADATA_MNT_POINT); |
| 247 | } |
| 248 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 249 | static bool read_and_install_user_ce_key(userid_t user_id, |
| 250 | const android::vold::KeyAuthentication& auth) { |
| 251 | if (s_ce_key_raw_refs.count(user_id) != 0) return true; |
| 252 | KeyBuffer ce_key; |
| 253 | if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false; |
| 254 | std::string ce_raw_ref; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 255 | if (is_wrapped_key_supported()) { |
| 256 | KeyBuffer ephemeral_wrapped_key; |
| 257 | if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_key)) { |
| 258 | LOG(ERROR) << "Failed to export ce key"; |
| 259 | return false; |
| 260 | } |
| 261 | ce_key = std::move(ephemeral_wrapped_key); |
| 262 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 263 | if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false; |
| 264 | s_ce_keys[user_id] = std::move(ce_key); |
| 265 | s_ce_key_raw_refs[user_id] = ce_raw_ref; |
| 266 | LOG(DEBUG) << "Installed ce key for user " << user_id; |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid) { |
| 271 | LOG(DEBUG) << "Preparing: " << dir; |
| 272 | if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) { |
| 273 | PLOG(ERROR) << "Failed to prepare " << dir; |
| 274 | return false; |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | static bool destroy_dir(const std::string& dir) { |
| 280 | LOG(DEBUG) << "Destroying: " << dir; |
| 281 | if (rmdir(dir.c_str()) != 0 && errno != ENOENT) { |
| 282 | PLOG(ERROR) << "Failed to destroy " << dir; |
| 283 | return false; |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | // NB this assumes that there is only one thread listening for crypt commands, because |
| 289 | // it creates keys in a fixed location. |
| 290 | static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) { |
| 291 | KeyBuffer de_key, ce_key; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 292 | |
| 293 | if(is_wrapped_key_supported()) { |
| 294 | if (!generateWrappedKey(user_id, android::vold::KeyType::DE_USER, &de_key)) return false; |
| 295 | if (!generateWrappedKey(user_id, android::vold::KeyType::CE_USER, &ce_key)) return false; |
| 296 | } else { |
| 297 | if (!android::vold::randomKey(&de_key)) return false; |
| 298 | if (!android::vold::randomKey(&ce_key)) return false; |
| 299 | } |
| 300 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 301 | if (create_ephemeral) { |
| 302 | // If the key should be created as ephemeral, don't store it. |
| 303 | s_ephemeral_users.insert(user_id); |
| 304 | } else { |
| 305 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 306 | if (!prepare_dir(directory_path, 0700, AID_ROOT, AID_ROOT)) return false; |
| 307 | auto const paths = get_ce_key_paths(directory_path); |
| 308 | std::string ce_key_path; |
| 309 | if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; |
| 310 | if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, |
| 311 | ce_key)) |
| 312 | return false; |
| 313 | fixate_user_ce_key(directory_path, ce_key_path, paths); |
| 314 | // Write DE key second; once this is written, all is good. |
| 315 | if (!android::vold::storeKeyAtomically(get_de_key_path(user_id), user_key_temp, |
| 316 | kEmptyAuthentication, de_key)) |
| 317 | return false; |
| 318 | } |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 319 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 320 | std::string ce_raw_ref; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 321 | if (is_wrapped_key_supported()) { |
| 322 | KeyBuffer ephemeral_wrapped_de_key; |
| 323 | KeyBuffer ephemeral_wrapped_ce_key; |
| 324 | |
| 325 | /* Export and install the DE keys */ |
| 326 | if (!getEphemeralWrappedKey(KeyFormat::RAW, de_key, &ephemeral_wrapped_de_key)) { |
| 327 | LOG(ERROR) << "Failed to export de_key"; |
| 328 | return false; |
| 329 | } |
| 330 | /* Export and install the CE keys */ |
| 331 | if (!getEphemeralWrappedKey(KeyFormat::RAW, ce_key, &ephemeral_wrapped_ce_key)) { |
| 332 | LOG(ERROR) << "Failed to export de_key"; |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | de_key = std::move(ephemeral_wrapped_de_key); |
| 337 | ce_key = std::move(ephemeral_wrapped_ce_key); |
| 338 | } |
| 339 | if (!android::vold::installKey(de_key, &de_raw_ref)) return false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 340 | if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 341 | s_ce_keys[user_id] = std::move(ce_key); |
| 342 | |
| 343 | s_de_key_raw_refs[user_id] = de_raw_ref; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 344 | s_ce_key_raw_refs[user_id] = ce_raw_ref; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 345 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 346 | LOG(DEBUG) << "Created keys for user " << user_id; |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id, |
| 351 | std::string* raw_ref) { |
| 352 | auto refi = key_map.find(user_id); |
| 353 | if (refi == key_map.end()) { |
| 354 | LOG(DEBUG) << "Cannot find key for " << user_id; |
| 355 | return false; |
| 356 | } |
| 357 | *raw_ref = refi->second; |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) { |
| 362 | if (!ReadDefaultFstab(&fstab_default)) { |
| 363 | PLOG(ERROR) << "Failed to open default fstab"; |
| 364 | return; |
| 365 | } |
| 366 | auto entry = android::fs_mgr::GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT); |
| 367 | if (entry == nullptr) { |
| 368 | LOG(ERROR) << "get_data_file_encryption_modes::failed\n"; |
| 369 | return; |
| 370 | } |
| 371 | key_ref->contents_mode = entry->file_contents_mode; |
| 372 | key_ref->filenames_mode = entry->file_names_mode; |
| 373 | } |
| 374 | |
| 375 | static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) { |
| 376 | return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(), |
| 377 | key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(), |
| 378 | key_ref.filenames_mode.c_str()) == 0; |
| 379 | } |
| 380 | |
| 381 | static bool is_numeric(const char* name) { |
| 382 | for (const char* p = name; *p != '\0'; p++) { |
| 383 | if (!isdigit(*p)) return false; |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | static bool load_all_de_keys() { |
| 389 | auto de_dir = user_key_dir + "/de"; |
| 390 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir); |
| 391 | if (!dirp) { |
| 392 | PLOG(ERROR) << "Unable to read de key directory"; |
| 393 | return false; |
| 394 | } |
| 395 | for (;;) { |
| 396 | errno = 0; |
| 397 | auto entry = readdir(dirp.get()); |
| 398 | if (!entry) { |
| 399 | if (errno) { |
| 400 | PLOG(ERROR) << "Unable to read de key directory"; |
| 401 | return false; |
| 402 | } |
| 403 | break; |
| 404 | } |
| 405 | if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) { |
| 406 | LOG(DEBUG) << "Skipping non-de-key " << entry->d_name; |
| 407 | continue; |
| 408 | } |
| 409 | userid_t user_id = std::stoi(entry->d_name); |
| 410 | if (s_de_key_raw_refs.count(user_id) == 0) { |
| 411 | auto key_path = de_dir + "/" + entry->d_name; |
| 412 | KeyBuffer key; |
| 413 | if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false; |
| 414 | std::string raw_ref; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 415 | if (is_wrapped_key_supported()) { |
| 416 | KeyBuffer ephemeral_wrapped_key; |
| 417 | if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) { |
| 418 | LOG(ERROR) << "Failed to export de_key in create_and_install_user_keys"; |
| 419 | return false; |
| 420 | } |
| 421 | key = std::move(ephemeral_wrapped_key); |
| 422 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 423 | if (!android::vold::installKey(key, &raw_ref)) return false; |
| 424 | s_de_key_raw_refs[user_id] = raw_ref; |
| 425 | LOG(DEBUG) << "Installed de key for user " << user_id; |
| 426 | } |
| 427 | } |
| 428 | // fscrypt:TODO: go through all DE directories, ensure that all user dirs have the |
| 429 | // correct policy set on them, and that no rogue ones exist. |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | bool fscrypt_initialize_systemwide_keys() { |
| 434 | LOG(INFO) << "fscrypt_initialize_systemwide_keys"; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 435 | bool wrapped_key_supported = false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 436 | |
| 437 | if (s_systemwide_keys_initialized) { |
| 438 | LOG(INFO) << "Already initialized"; |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | PolicyKeyRef device_ref; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 443 | wrapped_key_supported = is_wrapped_key_supported(); |
| 444 | |
| 445 | if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, |
| 446 | device_key_path, device_key_temp, |
| 447 | &device_ref.key_raw_ref, wrapped_key_supported)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 448 | return false; |
| 449 | get_data_file_encryption_modes(&device_ref); |
| 450 | |
| 451 | std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode; |
| 452 | std::string mode_filename = std::string("/data") + fscrypt_key_mode; |
| 453 | if (!android::vold::writeStringToFile(modestring, mode_filename)) return false; |
| 454 | |
| 455 | std::string ref_filename = std::string("/data") + fscrypt_key_ref; |
| 456 | if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false; |
| 457 | LOG(INFO) << "Wrote system DE key reference to:" << ref_filename; |
| 458 | |
| 459 | KeyBuffer per_boot_key; |
| 460 | if (!android::vold::randomKey(&per_boot_key)) return false; |
| 461 | std::string per_boot_raw_ref; |
| 462 | if (!android::vold::installKey(per_boot_key, &per_boot_raw_ref)) return false; |
| 463 | std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref; |
| 464 | if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false; |
| 465 | LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename; |
| 466 | |
| 467 | if (!android::vold::FsyncDirectory(device_key_dir)) return false; |
| 468 | s_systemwide_keys_initialized = true; |
| 469 | de_raw_ref = device_ref.key_raw_ref; |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | bool fscrypt_init_user0() { |
| 474 | if (!ReadDefaultFstab(&fstab_default)) { |
| 475 | PLOG(ERROR) << "Failed to open default fstab"; |
| 476 | return -1; |
| 477 | } |
| 478 | if (fscrypt_is_native()) { |
| 479 | if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return false; |
| 480 | if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return false; |
| 481 | if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return false; |
| 482 | if (!android::vold::pathExists(get_de_key_path(0))) { |
| 483 | if (!create_and_install_user_keys(0, false)) return false; |
| 484 | } |
| 485 | // TODO: switch to loading only DE_0 here once framework makes |
| 486 | // explicit calls to install DE keys for secondary users |
| 487 | if (!load_all_de_keys()) return false; |
| 488 | } |
| 489 | // We can only safely prepare DE storage here, since CE keys are probably |
| 490 | // entangled with user credentials. The framework will always prepare CE |
| 491 | // storage once CE keys are installed. |
| 492 | if (!fscrypt_prepare_user_storage("", 0, 0, STORAGE_FLAG_DE)) { |
| 493 | LOG(ERROR) << "Failed to prepare user 0 storage"; |
| 494 | return false; |
| 495 | } |
| 496 | // If this is a non-FBE device that recently left an emulated mode, |
| 497 | // restore user data directories to known-good state. |
| 498 | if (!fscrypt_is_native() && !fscrypt_is_emulated()) { |
| 499 | fscrypt_unlock_user_key(0, 0, "!", "!"); |
| 500 | } |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) { |
| 506 | LOG(DEBUG) << "fscrypt_vold_create_user_key for " << user_id << " serial " << serial; |
| 507 | if (!fscrypt_is_native()) { |
| 508 | return true; |
| 509 | } |
| 510 | // FIXME test for existence of key that is not loaded yet |
| 511 | if (s_ce_key_raw_refs.count(user_id) != 0) { |
| 512 | LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id |
| 513 | << " serial " << serial; |
| 514 | // FIXME should we fail the command? |
| 515 | return true; |
| 516 | } |
| 517 | if (!create_and_install_user_keys(user_id, ephemeral)) { |
| 518 | return false; |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | static void drop_caches() { |
| 524 | // Clean any dirty pages (otherwise they won't be dropped). |
| 525 | sync(); |
| 526 | // Drop inode and page caches. |
| 527 | if (!android::vold::writeStringToFile("3", "/proc/sys/vm/drop_caches")) { |
| 528 | PLOG(ERROR) << "Failed to drop caches during key eviction"; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static bool evict_ce_key(userid_t user_id) { |
| 533 | s_ce_keys.erase(user_id); |
| 534 | bool success = true; |
| 535 | std::string raw_ref; |
| 536 | // If we haven't loaded the CE key, no need to evict it. |
| 537 | if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) { |
| 538 | success &= android::vold::evictKey(raw_ref); |
| 539 | drop_caches(); |
| 540 | } |
| 541 | s_ce_key_raw_refs.erase(user_id); |
| 542 | return success; |
| 543 | } |
| 544 | |
| 545 | bool fscrypt_destroy_user_key(userid_t user_id) { |
| 546 | LOG(DEBUG) << "fscrypt_destroy_user_key(" << user_id << ")"; |
| 547 | if (!fscrypt_is_native()) { |
| 548 | return true; |
| 549 | } |
| 550 | bool success = true; |
| 551 | std::string raw_ref; |
| 552 | success &= evict_ce_key(user_id); |
| 553 | success &= |
| 554 | lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref); |
| 555 | s_de_key_raw_refs.erase(user_id); |
| 556 | auto it = s_ephemeral_users.find(user_id); |
| 557 | if (it != s_ephemeral_users.end()) { |
| 558 | s_ephemeral_users.erase(it); |
| 559 | } else { |
| 560 | for (auto const path : get_ce_key_paths(get_ce_key_directory_path(user_id))) { |
| 561 | success &= android::vold::destroyKey(path); |
| 562 | } |
| 563 | auto de_key_path = get_de_key_path(user_id); |
| 564 | if (android::vold::pathExists(de_key_path)) { |
| 565 | success &= android::vold::destroyKey(de_key_path); |
| 566 | } else { |
| 567 | LOG(INFO) << "Not present so not erasing: " << de_key_path; |
| 568 | } |
| 569 | } |
| 570 | return success; |
| 571 | } |
| 572 | |
| 573 | static bool emulated_lock(const std::string& path) { |
| 574 | if (chmod(path.c_str(), 0000) != 0) { |
| 575 | PLOG(ERROR) << "Failed to chmod " << path; |
| 576 | return false; |
| 577 | } |
| 578 | #if EMULATED_USES_SELINUX |
| 579 | if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) { |
| 580 | PLOG(WARNING) << "Failed to setfilecon " << path; |
| 581 | return false; |
| 582 | } |
| 583 | #endif |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | static bool emulated_unlock(const std::string& path, mode_t mode) { |
| 588 | if (chmod(path.c_str(), mode) != 0) { |
| 589 | PLOG(ERROR) << "Failed to chmod " << path; |
| 590 | // FIXME temporary workaround for b/26713622 |
| 591 | if (fscrypt_is_emulated()) return false; |
| 592 | } |
| 593 | #if EMULATED_USES_SELINUX |
| 594 | if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) { |
| 595 | PLOG(WARNING) << "Failed to restorecon " << path; |
| 596 | // FIXME temporary workaround for b/26713622 |
| 597 | if (fscrypt_is_emulated()) return false; |
| 598 | } |
| 599 | #endif |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | static bool parse_hex(const std::string& hex, std::string* result) { |
| 604 | if (hex == "!") { |
| 605 | *result = ""; |
| 606 | return true; |
| 607 | } |
| 608 | if (android::vold::HexToStr(hex, *result) != 0) { |
| 609 | LOG(ERROR) << "Invalid FBE hex string"; // Don't log the string for security reasons |
| 610 | return false; |
| 611 | } |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) { |
| 616 | return misc_path + "/vold/volume_keys/" + volume_uuid + "/default"; |
| 617 | } |
| 618 | |
| 619 | static std::string volume_secdiscardable_path(const std::string& volume_uuid) { |
| 620 | return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable"; |
| 621 | } |
| 622 | |
| 623 | static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid, |
| 624 | PolicyKeyRef* key_ref) { |
| 625 | auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); |
| 626 | std::string secdiscardable_hash; |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 627 | bool wrapped_key_supported = false; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 628 | if (android::vold::pathExists(secdiscardable_path)) { |
| 629 | if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash)) |
| 630 | return false; |
| 631 | } else { |
| 632 | if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) { |
| 633 | PLOG(ERROR) << "Creating directories for: " << secdiscardable_path; |
| 634 | return false; |
| 635 | } |
| 636 | if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash)) |
| 637 | return false; |
| 638 | } |
| 639 | auto key_path = volkey_path(misc_path, volume_uuid); |
| 640 | if (fs_mkdirs(key_path.c_str(), 0700) != 0) { |
| 641 | PLOG(ERROR) << "Creating directories for: " << key_path; |
| 642 | return false; |
| 643 | } |
| 644 | android::vold::KeyAuthentication auth("", secdiscardable_hash); |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 645 | wrapped_key_supported = is_wrapped_key_supported_external(); |
| 646 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 647 | if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp", |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 648 | &key_ref->key_raw_ref, wrapped_key_supported)) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 649 | return false; |
| 650 | key_ref->contents_mode = |
| 651 | android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts"); |
| 652 | key_ref->filenames_mode = |
| 653 | android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh"); |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) { |
| 658 | auto path = volkey_path(misc_path, volume_uuid); |
| 659 | if (!android::vold::pathExists(path)) return true; |
| 660 | return android::vold::destroyKey(path); |
| 661 | } |
| 662 | |
| 663 | bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex, |
| 664 | const std::string& secret_hex) { |
| 665 | LOG(DEBUG) << "fscrypt_add_user_key_auth " << user_id << " serial=" << serial |
| 666 | << " token_present=" << (token_hex != "!"); |
| 667 | if (!fscrypt_is_native()) return true; |
| 668 | if (s_ephemeral_users.count(user_id) != 0) return true; |
| 669 | std::string token, secret; |
| 670 | if (!parse_hex(token_hex, &token)) return false; |
| 671 | if (!parse_hex(secret_hex, &secret)) return false; |
| 672 | auto auth = |
| 673 | secret.empty() ? kEmptyAuthentication : android::vold::KeyAuthentication(token, secret); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 674 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 675 | auto const paths = get_ce_key_paths(directory_path); |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 676 | |
| 677 | KeyBuffer ce_key; |
| 678 | if(is_wrapped_key_supported()) { |
| 679 | std::string ce_key_current_path = get_ce_key_current_path(directory_path); |
| 680 | if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) { |
| 681 | LOG(DEBUG) << "Successfully retrieved key"; |
| 682 | } else { |
| 683 | if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) { |
| 684 | LOG(DEBUG) << "Successfully retrieved key"; |
| 685 | } |
| 686 | } |
| 687 | } else { |
| 688 | auto it = s_ce_keys.find(user_id); |
| 689 | if (it == s_ce_keys.end()) { |
| 690 | LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id; |
| 691 | return false; |
| 692 | } |
| 693 | ce_key = it->second; |
| 694 | } |
| 695 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 696 | std::string ce_key_path; |
| 697 | if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; |
| 698 | if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false; |
| 699 | if (!android::vold::FsyncDirectory(directory_path)) return false; |
| 700 | return true; |
| 701 | } |
| 702 | |
mauronofrio matarrese | 7982032 | 2020-05-25 19:48:56 +0200 | [diff] [blame] | 703 | bool fscrypt_clear_user_key_auth(userid_t user_id, int serial, const std::string& token_hex, |
| 704 | const std::string& secret_hex) { |
| 705 | LOG(DEBUG) << "fscrypt_clear_user_key_auth " << user_id << " serial=" << serial |
| 706 | << " token_present=" << (token_hex != "!"); |
| 707 | if (!fscrypt_is_native()) return true; |
| 708 | if (s_ephemeral_users.count(user_id) != 0) return true; |
| 709 | std::string token, secret; |
| 710 | |
| 711 | if (!parse_hex(token_hex, &token)) return false; |
| 712 | if (!parse_hex(secret_hex, &secret)) return false; |
| 713 | |
| 714 | if (is_wrapped_key_supported()) { |
| 715 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 716 | auto const paths = get_ce_key_paths(directory_path); |
| 717 | |
| 718 | KeyBuffer ce_key; |
| 719 | std::string ce_key_current_path = get_ce_key_current_path(directory_path); |
| 720 | |
| 721 | auto auth = android::vold::KeyAuthentication(token, secret); |
| 722 | /* Retrieve key while removing a pin. A secret is needed */ |
| 723 | if (android::vold::retrieveKey(ce_key_current_path, auth, &ce_key)) { |
| 724 | LOG(DEBUG) << "Successfully retrieved key"; |
| 725 | } else { |
| 726 | /* Retrieve key when going None to swipe and vice versa when a |
| 727 | synthetic password is present */ |
| 728 | if (android::vold::retrieveKey(ce_key_current_path, kEmptyAuthentication, &ce_key)) { |
| 729 | LOG(DEBUG) << "Successfully retrieved key"; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | std::string ce_key_path; |
| 734 | if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; |
| 735 | if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, kEmptyAuthentication, ce_key)) |
| 736 | return false; |
| 737 | } else { |
| 738 | if(!fscrypt_add_user_key_auth(user_id, serial, "!", "!")) return false; |
| 739 | } |
| 740 | return true; |
| 741 | } |
| 742 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 743 | bool fscrypt_fixate_newest_user_key_auth(userid_t user_id) { |
| 744 | LOG(DEBUG) << "fscrypt_fixate_newest_user_key_auth " << user_id; |
| 745 | if (!fscrypt_is_native()) return true; |
| 746 | if (s_ephemeral_users.count(user_id) != 0) return true; |
| 747 | auto const directory_path = get_ce_key_directory_path(user_id); |
| 748 | auto const paths = get_ce_key_paths(directory_path); |
| 749 | if (paths.empty()) { |
| 750 | LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id; |
| 751 | return false; |
| 752 | } |
| 753 | fixate_user_ce_key(directory_path, paths[0], paths); |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | // TODO: rename to 'install' for consistency, and take flags to know which keys to install |
| 758 | bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex, |
| 759 | const std::string& secret_hex) { |
| 760 | LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial |
| 761 | << " token_present=" << (token_hex != "!"); |
| 762 | if (fscrypt_is_native()) { |
| 763 | if (s_ce_key_raw_refs.count(user_id) != 0) { |
| 764 | LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id; |
| 765 | return true; |
| 766 | } |
| 767 | std::string token, secret; |
| 768 | if (!parse_hex(token_hex, &token)) return false; |
| 769 | if (!parse_hex(secret_hex, &secret)) return false; |
| 770 | android::vold::KeyAuthentication auth(token, secret); |
| 771 | if (!read_and_install_user_ce_key(user_id, auth)) { |
| 772 | LOG(ERROR) << "Couldn't read key for " << user_id; |
| 773 | return false; |
| 774 | } |
| 775 | } else { |
| 776 | // When in emulation mode, we just use chmod. However, we also |
| 777 | // unlock directories when not in emulation mode, to bring devices |
| 778 | // back into a known-good state. |
| 779 | if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) || |
| 780 | !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) || |
| 781 | !emulated_unlock(android::vold::BuildDataMediaCePath("", user_id), 0770) || |
| 782 | !emulated_unlock(android::vold::BuildDataUserCePath("", user_id), 0771)) { |
| 783 | LOG(ERROR) << "Failed to unlock user " << user_id; |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | // TODO: rename to 'evict' for consistency |
| 791 | bool fscrypt_lock_user_key(userid_t user_id) { |
| 792 | LOG(DEBUG) << "fscrypt_lock_user_key " << user_id; |
| 793 | if (fscrypt_is_native()) { |
| 794 | return evict_ce_key(user_id); |
| 795 | } else if (fscrypt_is_emulated()) { |
| 796 | // When in emulation mode, we just use chmod |
| 797 | if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) || |
| 798 | !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) || |
| 799 | !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) || |
| 800 | !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) { |
| 801 | LOG(ERROR) << "Failed to lock user " << user_id; |
| 802 | return false; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid, |
| 810 | userid_t user_id, int flags) { |
| 811 | if (0 != android::vold::ForkExecvp( |
| 812 | std::vector<std::string>{prepare_subdirs_path, action, volume_uuid, |
| 813 | std::to_string(user_id), std::to_string(flags)})) { |
| 814 | LOG(ERROR) << "vold_prepare_subdirs failed"; |
| 815 | return false; |
| 816 | } |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial, |
| 821 | int flags) { |
| 822 | LOG(DEBUG) << "fscrypt_prepare_user_storage for volume " << escape_empty(volume_uuid) |
| 823 | << ", user " << user_id << ", serial " << serial << ", flags " << flags; |
| 824 | |
| 825 | if (flags & STORAGE_FLAG_DE) { |
| 826 | // DE_sys key |
| 827 | auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id); |
| 828 | auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id); |
| 829 | auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); |
| 830 | |
| 831 | // DE_n key |
| 832 | auto system_de_path = android::vold::BuildDataSystemDePath(user_id); |
| 833 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 834 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 835 | auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id); |
| 836 | |
| 837 | if (volume_uuid.empty()) { |
| 838 | if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false; |
| 839 | #if MANAGE_MISC_DIRS |
| 840 | if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM), |
| 841 | multiuser_get_uid(user_id, AID_EVERYBODY))) |
| 842 | return false; |
| 843 | #endif |
| 844 | if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
| 845 | |
| 846 | if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; |
| 847 | if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false; |
| 848 | if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false; |
| 849 | } |
| 850 | if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
| 851 | |
| 852 | if (fscrypt_is_native()) { |
| 853 | PolicyKeyRef de_ref; |
| 854 | if (volume_uuid.empty()) { |
| 855 | if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false; |
| 856 | get_data_file_encryption_modes(&de_ref); |
| 857 | if (!ensure_policy(de_ref, system_de_path)) return false; |
| 858 | if (!ensure_policy(de_ref, misc_de_path)) return false; |
| 859 | if (!ensure_policy(de_ref, vendor_de_path)) return false; |
| 860 | } else { |
| 861 | if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref)) return false; |
| 862 | } |
| 863 | if (!ensure_policy(de_ref, user_de_path)) return false; |
| 864 | } |
| 865 | } |
| 866 | if (flags & STORAGE_FLAG_CE) { |
| 867 | // CE_n key |
| 868 | auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); |
| 869 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 870 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 871 | auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id); |
| 872 | auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id); |
| 873 | |
| 874 | if (volume_uuid.empty()) { |
| 875 | if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; |
| 876 | if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false; |
| 877 | if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false; |
| 878 | } |
| 879 | if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false; |
| 880 | if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; |
| 881 | |
| 882 | if (fscrypt_is_native()) { |
| 883 | PolicyKeyRef ce_ref; |
| 884 | if (volume_uuid.empty()) { |
| 885 | if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false; |
| 886 | get_data_file_encryption_modes(&ce_ref); |
| 887 | if (!ensure_policy(ce_ref, system_ce_path)) return false; |
| 888 | if (!ensure_policy(ce_ref, misc_ce_path)) return false; |
| 889 | if (!ensure_policy(ce_ref, vendor_ce_path)) return false; |
| 890 | |
| 891 | } else { |
| 892 | if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref)) return false; |
| 893 | } |
| 894 | if (!ensure_policy(ce_ref, media_ce_path)) return false; |
| 895 | if (!ensure_policy(ce_ref, user_ce_path)) return false; |
| 896 | } |
| 897 | |
| 898 | if (volume_uuid.empty()) { |
| 899 | // Now that credentials have been installed, we can run restorecon |
| 900 | // over these paths |
| 901 | // NOTE: these paths need to be kept in sync with libselinux |
| 902 | android::vold::RestoreconRecursive(system_ce_path); |
| 903 | android::vold::RestoreconRecursive(vendor_ce_path); |
| 904 | android::vold::RestoreconRecursive(misc_ce_path); |
| 905 | } |
| 906 | } |
| 907 | if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false; |
| 908 | |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | bool fscrypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) { |
| 913 | LOG(DEBUG) << "fscrypt_destroy_user_storage for volume " << escape_empty(volume_uuid) |
| 914 | << ", user " << user_id << ", flags " << flags; |
| 915 | bool res = true; |
| 916 | |
| 917 | res &= prepare_subdirs("destroy", volume_uuid, user_id, flags); |
| 918 | |
| 919 | if (flags & STORAGE_FLAG_CE) { |
| 920 | // CE_n key |
| 921 | auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); |
| 922 | auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); |
| 923 | auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); |
| 924 | auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id); |
| 925 | auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id); |
| 926 | |
| 927 | res &= destroy_dir(media_ce_path); |
| 928 | res &= destroy_dir(user_ce_path); |
| 929 | if (volume_uuid.empty()) { |
| 930 | res &= destroy_dir(system_ce_path); |
| 931 | res &= destroy_dir(misc_ce_path); |
| 932 | res &= destroy_dir(vendor_ce_path); |
| 933 | } else { |
| 934 | if (fscrypt_is_native()) { |
| 935 | res &= destroy_volkey(misc_ce_path, volume_uuid); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if (flags & STORAGE_FLAG_DE) { |
| 941 | // DE_sys key |
| 942 | auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id); |
| 943 | auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id); |
| 944 | auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); |
| 945 | |
| 946 | // DE_n key |
| 947 | auto system_de_path = android::vold::BuildDataSystemDePath(user_id); |
| 948 | auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); |
| 949 | auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); |
| 950 | auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id); |
| 951 | |
| 952 | res &= destroy_dir(user_de_path); |
| 953 | if (volume_uuid.empty()) { |
| 954 | res &= destroy_dir(system_legacy_path); |
| 955 | #if MANAGE_MISC_DIRS |
| 956 | res &= destroy_dir(misc_legacy_path); |
| 957 | #endif |
| 958 | res &= destroy_dir(profiles_de_path); |
| 959 | res &= destroy_dir(system_de_path); |
| 960 | res &= destroy_dir(misc_de_path); |
| 961 | res &= destroy_dir(vendor_de_path); |
| 962 | } else { |
| 963 | if (fscrypt_is_native()) { |
| 964 | res &= destroy_volkey(misc_de_path, volume_uuid); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | return res; |
| 970 | } |
| 971 | |
| 972 | static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) { |
| 973 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(directory_path.c_str()), closedir); |
| 974 | if (!dirp) { |
| 975 | PLOG(ERROR) << "Unable to open directory: " + directory_path; |
| 976 | return false; |
| 977 | } |
| 978 | bool res = true; |
| 979 | for (;;) { |
| 980 | errno = 0; |
| 981 | auto const entry = readdir(dirp.get()); |
| 982 | if (!entry) { |
| 983 | if (errno) { |
| 984 | PLOG(ERROR) << "Unable to read directory: " + directory_path; |
| 985 | return false; |
| 986 | } |
| 987 | break; |
| 988 | } |
| 989 | if (entry->d_type != DT_DIR || entry->d_name[0] == '.') { |
| 990 | LOG(DEBUG) << "Skipping non-user " << entry->d_name; |
| 991 | continue; |
| 992 | } |
| 993 | res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid); |
| 994 | } |
| 995 | return res; |
| 996 | } |
| 997 | |
| 998 | bool fscrypt_destroy_volume_keys(const std::string& volume_uuid) { |
| 999 | bool res = true; |
| 1000 | LOG(DEBUG) << "fscrypt_destroy_volume_keys for volume " << escape_empty(volume_uuid); |
| 1001 | auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); |
| 1002 | res &= android::vold::runSecdiscardSingle(secdiscardable_path); |
| 1003 | res &= destroy_volume_keys("/data/misc_ce", volume_uuid); |
| 1004 | res &= destroy_volume_keys("/data/misc_de", volume_uuid); |
| 1005 | return res; |
| 1006 | } |