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