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