diff --git a/crypto/ext4crypt/Decrypt.cpp b/crypto/ext4crypt/Decrypt.cpp index fe270172..dcd390f1 100755 --- a/crypto/ext4crypt/Decrypt.cpp +++ b/crypto/ext4crypt/Decrypt.cpp @@ -549,16 +549,13 @@ std::string unwrapSyntheticPasswordBlob(const std::string& spblob_path, const st std::string disk_decryption_secret_key = ""; std::string keystore_alias_subid; - // Can be stored in user 0, so check for both. - if (!Find_Keystore_Alias_SubID_And_Prep_Files(user_id, keystore_alias_subid, handle_str) && - !Find_Keystore_Alias_SubID_And_Prep_Files(0, keystore_alias_subid, handle_str)) - { - printf("failed to scan keystore alias subid and prep keystore files\n"); - return disk_decryption_secret_key; - } + if (!Find_Keystore_Alias_SubID_And_Prep_Files(user_id, keystore_alias_subid, handle_str)) { + printf("failed to scan keystore alias subid and prep keystore files\n"); + return disk_decryption_secret_key; + } // First get the keystore service - sp binder = getKeystoreBinderRetry(); + sp binder = getKeystoreBinderRetry(); #ifdef USE_KEYSTORAGE_4 sp service = interface_cast(binder); #else @@ -1168,15 +1165,15 @@ bool Decrypt_User_Synth_Pass(const userid_t user_id, const std::string& Password printf("e4crypt_unlock_user_key returned fail\n"); return Free_Return(retval, weaver_key, &pwd); } -/*#ifdef USE_KEYSTORAGE_4 +#ifdef USE_KEYSTORAGE_4 if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { #else if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { #endif printf("failed to e4crypt_prepare_user_storage\n"); return Free_Return(retval, weaver_key, &pwd); - }*/ - printf("User %i Decrypted Successfully!\n", user_id); + } + printf("Decrypted Successfully!\n"); retval = true; return Free_Return(retval, weaver_key, &pwd); } @@ -1258,15 +1255,15 @@ bool Decrypt_User(const userid_t user_id, const std::string& Password) { printf("e4crypt_unlock_user_key returned fail\n"); return false; } -/*#ifdef USE_KEYSTORAGE_4 +#ifdef USE_KEYSTORAGE_4 if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { #else if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { #endif printf("failed to e4crypt_prepare_user_storage\n"); return false; - }*/ - printf("User %i Decrypted Successfully!\n", user_id); + } + printf("Decrypted Successfully!\n"); return true; } if (stat("/data/system_de/0/spblob", &st) == 0) { @@ -1346,14 +1343,14 @@ bool Decrypt_User(const userid_t user_id, const std::string& Password) { printf("e4crypt_unlock_user_key returned fail\n"); return false; } -/*#ifdef USE_KEYSTORAGE_4 +#ifdef USE_KEYSTORAGE_4 if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { #else if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { #endif printf("failed to e4crypt_prepare_user_storage\n"); return false; - }*/ - printf("User %i Decrypted Successfully!\n", user_id); + } + printf("Decrypted Successfully!\n"); return true; } diff --git a/crypto/ext4crypt/Ext4Crypt.cpp b/crypto/ext4crypt/Ext4Crypt.cpp index 9360b1a2..c9e71fd9 100644 --- a/crypto/ext4crypt/Ext4Crypt.cpp +++ b/crypto/ext4crypt/Ext4Crypt.cpp @@ -63,7 +63,6 @@ #define MANAGE_MISC_DIRS 0 #include -#include #include //#include @@ -317,6 +316,16 @@ bool lookup_key_ref(const std::map& key_map, userid_t use return true; } +static bool ensure_policy(const std::string& raw_ref __unused, const std::string& path) { + return true; + // ensure policy will set a policy if one is not set on an empty folder - we don't want to do this in recovery + /*if (e4crypt_policy_ensure(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) { + LOG(ERROR) << "Failed to set policy on: " << path << "\n"; + return false; + } + return true;*/ +} + static bool is_numeric(const char* name) { for (const char* p = name; *p != '\0'; p++) { if (!isdigit(*p)) return false; @@ -354,9 +363,6 @@ static bool load_all_de_keys() { if (!install_key(key, &raw_ref)) return false; s_de_key_raw_refs[user_id] = raw_ref; LOG(DEBUG) << "Installed de key for user " << user_id; - - std::string user_prop = "twrp.user." + std::to_string(user_id) + ".decrypt"; - property_set(user_prop.c_str(), "0"); } } // ext4enc:TODO: go through all DE directories, ensure that all user dirs have the @@ -405,6 +411,13 @@ bool e4crypt_init_user0() { // explicit calls to install DE keys for secondary users if (!load_all_de_keys()) return false; } + // We can only safely prepare DE storage here, since CE keys are probably + // entangled with user credentials. The framework will always prepare CE + // storage once CE keys are installed. + if (!e4crypt_prepare_user_storage(nullptr, 0, 0, FLAG_STORAGE_DE)) { + LOG(ERROR) << "Failed to prepare user 0 storage"; + return false; + } // If this is a non-FBE device that recently left an emulated mode, // restore user data directories to known-good state. @@ -445,6 +458,79 @@ bool e4crypt_unlock_user_key(userid_t user_id, int serial __unused, const char* } } else { printf("Emulation mode not supported in TWRP\n"); + // When in emulation mode, we just use chmod. However, we also + // unlock directories when not in emulation mode, to bring devices + // back into a known-good state. + /*if (!emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) || + !emulated_unlock(android::vold::BuildDataMiscCePath(user_id), 01771) || + !emulated_unlock(android::vold::BuildDataMediaCePath(nullptr, user_id), 0770) || + !emulated_unlock(android::vold::BuildDataUserCePath(nullptr, user_id), 0771)) { + LOG(ERROR) << "Failed to unlock user " << user_id; + return false; + }*/ } return true; } + +bool e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id, int serial __unused, + int flags) { + + if (flags & FLAG_STORAGE_DE) { + // DE_sys key + auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id); + auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id); + auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); + auto foreign_de_path = android::vold::BuildDataProfilesForeignDexDePath(user_id); + + // DE_n key + auto system_de_path = android::vold::BuildDataSystemDePath(user_id); + auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); + auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id); + + if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false; +#if MANAGE_MISC_DIRS + if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM), + multiuser_get_uid(user_id, AID_EVERYBODY))) return false; +#endif + if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(foreign_de_path, 0773, AID_SYSTEM, AID_SYSTEM)) return false; + + if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false; + if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + + // For now, FBE is only supported on internal storage + if (e4crypt_is_native() && volume_uuid == nullptr) { + std::string de_raw_ref; + if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_raw_ref)) return false; + if (!ensure_policy(de_raw_ref, system_de_path)) return false; + if (!ensure_policy(de_raw_ref, misc_de_path)) return false; + if (!ensure_policy(de_raw_ref, user_de_path)) return false; + } + } + + if (flags & FLAG_STORAGE_CE) { + // CE_n key + auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); + auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); + auto media_ce_path = android::vold::BuildDataMediaCePath(volume_uuid, user_id); + auto user_ce_path = android::vold::BuildDataUserCePath(volume_uuid, user_id); + + if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false; + if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false; + if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + + // For now, FBE is only supported on internal storage + if (e4crypt_is_native() && volume_uuid == nullptr) { + std::string ce_raw_ref; + if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_raw_ref)) return false; + if (!ensure_policy(ce_raw_ref, system_ce_path)) return false; + if (!ensure_policy(ce_raw_ref, misc_ce_path)) return false; + if (!ensure_policy(ce_raw_ref, media_ce_path)) return false; + if (!ensure_policy(ce_raw_ref, user_ce_path)) return false; + } + } + + return true; +} diff --git a/crypto/ext4crypt/Ext4Crypt.h b/crypto/ext4crypt/Ext4Crypt.h index beb5d8fe..57623e35 100644 --- a/crypto/ext4crypt/Ext4Crypt.h +++ b/crypto/ext4crypt/Ext4Crypt.h @@ -38,7 +38,7 @@ bool e4crypt_init_user0(); bool e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token, const char* secret); //bool e4crypt_lock_user_key(userid_t user_id); -//bool e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id, int serial, int flags); +bool e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id, int serial, int flags); //bool e4crypt_destroy_user_storage(const char* volume_uuid, userid_t user_id, int flags); bool lookup_key_ref(const std::map& key_map, userid_t user_id, diff --git a/crypto/ext4crypt/Ext4CryptPie.cpp b/crypto/ext4crypt/Ext4CryptPie.cpp index 48b4ec96..548e4e44 100644 --- a/crypto/ext4crypt/Ext4CryptPie.cpp +++ b/crypto/ext4crypt/Ext4CryptPie.cpp @@ -110,9 +110,9 @@ static bool e4crypt_is_emulated() { return property_get_bool("persist.sys.emulate_fbe", false); } -/*static const char* escape_empty(const std::string& value) { +static const char* escape_empty(const std::string& value) { return value.empty() ? "null" : value.c_str(); -}*/ +} static std::string get_de_key_path(userid_t user_id) { return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id); @@ -155,6 +155,23 @@ static std::string get_ce_key_current_path(const std::string& directory_path) { return directory_path + "/current"; } +/*static bool get_ce_key_new_path(const std::string& directory_path, + const std::vector& paths, + std::string *ce_key_path) { + if (paths.empty()) { + *ce_key_path = get_ce_key_current_path(directory_path); + return true; + } + for (unsigned int i = 0; i < UINT_MAX; i++) { + auto const candidate = StringPrintf("%s/cx%010u", directory_path.c_str(), i); + if (paths[0] < candidate) { + *ce_key_path = candidate; + return true; + } + } + return false; +}*/ + // Discard all keys but the named one; rename it to canonical name. // No point in acting on errors in this; ignore them. static void fixate_user_ce_key(const std::string& directory_path, const std::string &to_fix, @@ -242,6 +259,15 @@ static bool prepare_dir(const std::string& dir, mode_t mode, uid_t uid, gid_t gi return true; } +/*static bool destroy_dir(const std::string& dir) { + LOG(DEBUG) << "Destroying: " << dir; + if (rmdir(dir.c_str()) != 0 && errno != ENOENT) { + PLOG(ERROR) << "Failed to destroy " << dir; + return false; + } + return true; +}*/ + // NB this assumes that there is only one thread listening for crypt commands, because // it creates keys in a fixed location. static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) { @@ -301,6 +327,13 @@ static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) { android::base::GetProperty("fbe.filenames", "aes-256-heh"); } +static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) { + return true; + /*return e4crypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(), + key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(), + key_ref.filenames_mode.c_str()) == 0;*/ +} + static bool is_numeric(const char* name) { for (const char* p = name; *p != '\0'; p++) { if (!isdigit(*p)) return false; @@ -346,9 +379,6 @@ static bool load_all_de_keys() { if (!android::vold::installKey(key, &raw_ref)) return false; s_de_key_raw_refs[user_id] = raw_ref; LOG(DEBUG) << "Installed de key for user " << user_id << std::endl; - - std::string user_prop = "twrp.user." + std::to_string(user_id) + ".decrypt"; - property_set(user_prop.c_str(), "0"); } } // ext4enc:TODO: go through all DE directories, ensure that all user dirs have the @@ -405,6 +435,13 @@ bool e4crypt_init_user0() { // explicit calls to install DE keys for secondary users if (!load_all_de_keys()) return false; } + // We can only safely prepare DE storage here, since CE keys are probably + // entangled with user credentials. The framework will always prepare CE + // storage once CE keys are installed. + if (!e4crypt_prepare_user_storage("", 0, 0, /*android::os::IVold::*/STORAGE_FLAG_DE)) { + LOG(ERROR) << "Failed to prepare user 0 storage" << std::endl; + return false; + } // If this is a non-FBE device that recently left an emulated mode, // restore user data directories to known-good state. @@ -415,6 +452,92 @@ bool e4crypt_init_user0() { return true; } +/*bool e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) { + LOG(DEBUG) << "TWRP NOT e4crypt_vold_create_user_key for " << user_id << " serial " << serial; + return true; + if (!e4crypt_is_native()) { + return true; + } + // FIXME test for existence of key that is not loaded yet + if (s_ce_key_raw_refs.count(user_id) != 0) { + LOG(ERROR) << "Already exists, can't e4crypt_vold_create_user_key for " << user_id + << " serial " << serial; + // FIXME should we fail the command? + return true; + } + if (!create_and_install_user_keys(user_id, ephemeral)) { + return false; + } + return true; +} + +static void drop_caches() { + // Clean any dirty pages (otherwise they won't be dropped). + sync(); + // Drop inode and page caches. + if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { + PLOG(ERROR) << "Failed to drop caches during key eviction"; + } +} + +static bool evict_ce_key(userid_t user_id) { + LOG(ERROR) << "TWRP NOT evict_ce_key\n"; + return true; + s_ce_keys.erase(user_id); + bool success = true; + std::string raw_ref; + // If we haven't loaded the CE key, no need to evict it. + if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) { + success &= android::vold::evictKey(raw_ref); + drop_caches(); + } + s_ce_key_raw_refs.erase(user_id); + return success; +} + +bool e4crypt_destroy_user_key(userid_t user_id) { + LOG(DEBUG) << "NOT e4crypt_destroy_user_key(" << user_id << ")"; + return true; + if (!e4crypt_is_native()) { + return true; + } + bool success = true; + std::string raw_ref; + success &= evict_ce_key(user_id); + success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) + && android::vold::evictKey(raw_ref); + s_de_key_raw_refs.erase(user_id); + auto it = s_ephemeral_users.find(user_id); + if (it != s_ephemeral_users.end()) { + s_ephemeral_users.erase(it); + } else { + for (auto const path: get_ce_key_paths(get_ce_key_directory_path(user_id))) { + success &= android::vold::destroyKey(path); + } + auto de_key_path = get_de_key_path(user_id); + if (android::vold::pathExists(de_key_path)) { + success &= android::vold::destroyKey(de_key_path); + } else { + LOG(INFO) << "Not present so not erasing: " << de_key_path; + } + } + return success; +} + +static bool emulated_lock(const std::string& path) { + if (chmod(path.c_str(), 0000) != 0) { + PLOG(ERROR) << "Failed to chmod " << path; + return false; + } +#if EMULATED_USES_SELINUX + if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) { + PLOG(WARNING) << "Failed to setfilecon " << path; + return false; + } +#endif + return true; +}*/ + static bool emulated_unlock(const std::string& path, mode_t mode) { if (chmod(path.c_str(), mode) != 0) { PLOG(ERROR) << "Failed to chmod " << path << std::endl; @@ -443,6 +566,92 @@ static bool parse_hex(const std::string& hex, std::string* result) { return true; } +static std::string volkey_path(const std::string& misc_path, const std::string& volume_uuid) { + return misc_path + "/vold/volume_keys/" + volume_uuid + "/default"; +} + +static std::string volume_secdiscardable_path(const std::string& volume_uuid) { + return systemwide_volume_key_dir + "/" + volume_uuid + "/secdiscardable"; +} + +static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid, + PolicyKeyRef* key_ref) { + auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); + std::string secdiscardable_hash; + bool wrapped_key_supported = false; + if (android::vold::pathExists(secdiscardable_path)) { + if (!android::vold::readSecdiscardable(secdiscardable_path, &secdiscardable_hash)) + return false; + } else { + if (fs_mkdirs(secdiscardable_path.c_str(), 0700) != 0) { + PLOG(ERROR) << "Creating directories for: " << secdiscardable_path << std::endl; + return false; + } + if (!android::vold::createSecdiscardable(secdiscardable_path, &secdiscardable_hash)) + return false; + } + auto key_path = volkey_path(misc_path, volume_uuid); + if (fs_mkdirs(key_path.c_str(), 0700) != 0) { + PLOG(ERROR) << "Creating directories for: " << key_path << std::endl; + return false; + } + android::vold::KeyAuthentication auth("", secdiscardable_hash); + wrapped_key_supported = is_wrapped_key_supported_external(); + if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp", + &key_ref->key_raw_ref, wrapped_key_supported)) + return false; + key_ref->contents_mode = + android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts"); + key_ref->filenames_mode = + android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh"); + return true; +} + +/*static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) { + auto path = volkey_path(misc_path, volume_uuid); + if (!android::vold::pathExists(path)) return true; + return android::vold::destroyKey(path); +} + +bool e4crypt_add_user_key_auth(userid_t user_id, int serial, const std::string& token_hex, + const std::string& secret_hex) { + LOG(DEBUG) << "e4crypt_add_user_key_auth " << user_id << " serial=" << serial + << " token_present=" << (token_hex != "!"); + if (!e4crypt_is_native()) return true; + if (s_ephemeral_users.count(user_id) != 0) return true; + std::string token, secret; + if (!parse_hex(token_hex, &token)) return false; + if (!parse_hex(secret_hex, &secret)) return false; + auto auth = secret.empty() ? kEmptyAuthentication + : android::vold::KeyAuthentication(token, secret); + auto it = s_ce_keys.find(user_id); + if (it == s_ce_keys.end()) { + LOG(ERROR) << "Key not loaded into memory, can't change for user " << user_id; + return false; + } + const auto &ce_key = it->second; + auto const directory_path = get_ce_key_directory_path(user_id); + auto const paths = get_ce_key_paths(directory_path); + std::string ce_key_path; + if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; + if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false; + return true; +} + +bool e4crypt_fixate_newest_user_key_auth(userid_t user_id) { + LOG(DEBUG) << "e4crypt_fixate_newest_user_key_auth " << user_id; + if (!e4crypt_is_native()) return true; + if (s_ephemeral_users.count(user_id) != 0) return true; + auto const directory_path = get_ce_key_directory_path(user_id); + auto const paths = get_ce_key_paths(directory_path); + if (paths.empty()) { + LOG(ERROR) << "No ce keys present, cannot fixate for user " << user_id; + return false; + } + fixate_user_ce_key(directory_path, paths[0], paths); + return true; +}*/ + // TODO: rename to 'install' for consistency, and take flags to know which keys to install bool e4crypt_unlock_user_key(userid_t user_id, int serial, const std::string& token_hex, const std::string& secret_hex) { @@ -475,3 +684,224 @@ bool e4crypt_unlock_user_key(userid_t user_id, int serial, const std::string& to } return true; } + +// TODO: rename to 'evict' for consistency +/*bool e4crypt_lock_user_key(userid_t user_id) { + LOG(DEBUG) << "TWRP NOTe4crypt_lock_user_key " << user_id; + return true; + if (e4crypt_is_native()) { + return evict_ce_key(user_id); + } else if (e4crypt_is_emulated()) { + // When in emulation mode, we just use chmod + if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) || + !emulated_lock(android::vold::BuildDataMiscCePath(user_id)) || + !emulated_lock(android::vold::BuildDataMediaCePath("", user_id)) || + !emulated_lock(android::vold::BuildDataUserCePath("", user_id))) { + LOG(ERROR) << "Failed to lock user " << user_id; + return false; + } + } + + return true; +}*/ + +static bool prepare_subdirs(const std::string& action, const std::string& volume_uuid, + userid_t user_id, int flags) { + LOG(ERROR) << "not actually forking for vold_prepare_subdirs\n"; + return true; + /*if (0 != android::vold::ForkExecvp( + std::vector{prepare_subdirs_path, action, volume_uuid, + std::to_string(user_id), std::to_string(flags)})) { + LOG(ERROR) << "vold_prepare_subdirs failed"; + return false; + } + return true;*/ +} + +bool e4crypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial, + int flags) { + LOG(DEBUG) << "e4crypt_prepare_user_storage for volume " << escape_empty(volume_uuid) + << ", user " << user_id << ", serial " << serial << ", flags " << flags << std::endl; + + if (flags & /*android::os::IVold::*/STORAGE_FLAG_DE) { + // DE_sys key + auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id); + auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id); + auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); + + // DE_n key + auto system_de_path = android::vold::BuildDataSystemDePath(user_id); + auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); + auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); + auto user_de_path = android::vold::BuildDataUserDePath(nullptr, user_id); + + if (volume_uuid.empty()) { + if (!prepare_dir(system_legacy_path, 0700, AID_SYSTEM, AID_SYSTEM)) return false; +#if MANAGE_MISC_DIRS + if (!prepare_dir(misc_legacy_path, 0750, multiuser_get_uid(user_id, AID_SYSTEM), + multiuser_get_uid(user_id, AID_EVERYBODY))) return false; +#endif + if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false; + if (!prepare_dir(vendor_de_path, 0771, AID_ROOT, AID_ROOT)) return false; + } + if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + + if (e4crypt_is_native()) { + PolicyKeyRef de_ref; + if (volume_uuid.empty()) { + if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false; + get_data_file_encryption_modes(&de_ref); + if (!ensure_policy(de_ref, system_de_path)) return false; + if (!ensure_policy(de_ref, misc_de_path)) return false; + if (!ensure_policy(de_ref, vendor_de_path)) return false; + } else { + if (!read_or_create_volkey(misc_de_path, nullptr, &de_ref)) return false; + } + if (!ensure_policy(de_ref, user_de_path)) return false; + } + } + + if (flags & /*android::os::IVold::*/STORAGE_FLAG_CE) { + // CE_n key + auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); + auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); + auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); + auto media_ce_path = android::vold::BuildDataMediaCePath(nullptr, user_id); + auto user_ce_path = android::vold::BuildDataUserCePath(nullptr, user_id); + + if (volume_uuid.empty()) { + if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false; + if (!prepare_dir(misc_ce_path, 01771, AID_SYSTEM, AID_MISC)) return false; + if (!prepare_dir(vendor_ce_path, 0771, AID_ROOT, AID_ROOT)) return false; + } + if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return false; + if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false; + + if (e4crypt_is_native()) { + PolicyKeyRef ce_ref; + if (volume_uuid.empty()) { + if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false; + get_data_file_encryption_modes(&ce_ref); + if (!ensure_policy(ce_ref, system_ce_path)) return false; + if (!ensure_policy(ce_ref, misc_ce_path)) return false; + if (!ensure_policy(ce_ref, vendor_ce_path)) return false; + } else { + if (!read_or_create_volkey(misc_ce_path, nullptr, &ce_ref)) return false; + } + if (!ensure_policy(ce_ref, media_ce_path)) return false; + if (!ensure_policy(ce_ref, user_ce_path)) return false; + } + + if (volume_uuid.empty()) { + // Now that credentials have been installed, we can run restorecon + // over these paths + // NOTE: these paths need to be kept in sync with libselinux + //android::vold::RestoreconRecursive(system_ce_path); + //android::vold::RestoreconRecursive(misc_ce_path); + } + } + if (!prepare_subdirs("prepare", volume_uuid, user_id, flags)) return false; + return true; +} + +/*bool e4crypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags) { + LOG(DEBUG) << "TWRP NOT e4crypt_destroy_user_storage for volume " << escape_empty(volume_uuid) + << ", user " << user_id << ", flags " << flags; + bool res = true; + return res; + + res &= prepare_subdirs("destroy", volume_uuid, user_id, flags); + + if (flags & android::os::IVold::STORAGE_FLAG_CE) { + // CE_n key + auto system_ce_path = android::vold::BuildDataSystemCePath(user_id); + auto misc_ce_path = android::vold::BuildDataMiscCePath(user_id); + auto vendor_ce_path = android::vold::BuildDataVendorCePath(user_id); + auto media_ce_path = android::vold::BuildDataMediaCePath(nullptr, user_id); + auto user_ce_path = android::vold::BuildDataUserCePath(nullptr, user_id); + + res &= destroy_dir(media_ce_path); + res &= destroy_dir(user_ce_path); + if (volume_uuid.empty()) { + res &= destroy_dir(system_ce_path); + res &= destroy_dir(misc_ce_path); + res &= destroy_dir(vendor_ce_path); + } else { + if (e4crypt_is_native()) { + res &= destroy_volkey(misc_ce_path, volume_uuid); + } + } + } + + if (flags & android::os::IVold::STORAGE_FLAG_DE) { + // DE_sys key + auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id); + auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id); + auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id); + + // DE_n key + auto system_de_path = android::vold::BuildDataSystemDePath(user_id); + auto misc_de_path = android::vold::BuildDataMiscDePath(user_id); + auto vendor_de_path = android::vold::BuildDataVendorDePath(user_id); + auto user_de_path = android::vold::BuildDataUserDePath(nullptr, user_id); + + res &= destroy_dir(user_de_path); + if (volume_uuid.empty()) { + res &= destroy_dir(system_legacy_path); +#if MANAGE_MISC_DIRS + res &= destroy_dir(misc_legacy_path); +#endif + res &= destroy_dir(profiles_de_path); + res &= destroy_dir(system_de_path); + res &= destroy_dir(misc_de_path); + res &= destroy_dir(vendor_de_path); + } else { + if (e4crypt_is_native()) { + res &= destroy_volkey(misc_de_path, volume_uuid); + } + } + } + + return res; +} + +static bool destroy_volume_keys(const std::string& directory_path, const std::string& volume_uuid) { + LOG(ERROR) << "TWRP NOT destroy_volume_keys\n"; + return true; + auto dirp = std::unique_ptr(opendir(directory_path.c_str()), closedir); + if (!dirp) { + PLOG(ERROR) << "Unable to open directory: " + directory_path; + return false; + } + bool res = true; + for (;;) { + errno = 0; + auto const entry = readdir(dirp.get()); + if (!entry) { + if (errno) { + PLOG(ERROR) << "Unable to read directory: " + directory_path; + return false; + } + break; + } + if (entry->d_type != DT_DIR || entry->d_name[0] == '.') { + LOG(DEBUG) << "Skipping non-user " << entry->d_name; + continue; + } + res &= destroy_volkey(directory_path + "/" + entry->d_name, volume_uuid); + } + return res; +} + +bool e4crypt_destroy_volume_keys(const std::string& volume_uuid) { + bool res = true; + LOG(DEBUG) << "TWRP NOT e4crypt_destroy_volume_keys for volume " << escape_empty(volume_uuid); + /*return res; + auto secdiscardable_path = volume_secdiscardable_path(volume_uuid); + res &= android::vold::runSecdiscardSingle(secdiscardable_path); + res &= destroy_volume_keys("/data/misc_ce", volume_uuid); + res &= destroy_volume_keys("/data/misc_de", volume_uuid); + return res; +}*/ diff --git a/crypto/ext4crypt/Ext4CryptPie.h b/crypto/ext4crypt/Ext4CryptPie.h index 7236bc00..d6d6ecf2 100644 --- a/crypto/ext4crypt/Ext4CryptPie.h +++ b/crypto/ext4crypt/Ext4CryptPie.h @@ -32,8 +32,8 @@ bool e4crypt_unlock_user_key(userid_t user_id, int serial, const std::string& to const std::string& secret); //bool e4crypt_lock_user_key(userid_t user_id); -/*bool e4crypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial, - int flags);*/ +bool e4crypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_id, int serial, + int flags); /*bool e4crypt_destroy_user_storage(const std::string& volume_uuid, userid_t user_id, int flags); bool e4crypt_destroy_volume_keys(const std::string& volume_uuid);*/ diff --git a/gui/action.cpp b/gui/action.cpp index 65526b99..b7d9119e 100755 --- a/gui/action.cpp +++ b/gui/action.cpp @@ -64,7 +64,6 @@ std::set GUIAction::setActionsRunningInCallerThread; static string zip_queue[10]; static int zip_queue_index; pid_t sideload_child_pid; -extern std::vector Users_List; static void *ActionThread_work_wrapper(void *data); @@ -1503,31 +1502,12 @@ int GUIAction::decrypt(std::string arg __unused) simulate_progress_bar(); } else { string Password; - string userID; DataManager::GetValue("tw_crypto_password", Password); - - if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE - DataManager::GetValue("tw_crypto_user_id", userID); - if (userID != "") { - op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str())); - if (userID != "0") { - if (op_status != 0) - op_status = 1; - operation_end(op_status); - return 0; - } - } else { - LOGINFO("User ID not found\n"); - op_status = 1; - } - ::sleep(1); - } else { // for FDE - op_status = PartitionManager.Decrypt_Device(Password); - } - + op_status = PartitionManager.Decrypt_Device(Password); if (op_status != 0) op_status = 1; else { + DataManager::SetValue(TW_IS_ENCRYPTED, 0); int has_datamedia; diff --git a/gui/gui.hpp b/gui/gui.hpp index 7e4ee4ea..d5b9553d 100644 --- a/gui/gui.hpp +++ b/gui/gui.hpp @@ -28,7 +28,6 @@ void gui_warn(const char* text); void gui_err(const char* text); void gui_highlight(const char* text); void gui_msg(Message msg); -void gui_err(Message msg); std::string gui_parse_text(std::string inText); std::string gui_lookup(const std::string& resource_name, const std::string& default_value); diff --git a/gui/listbox.cpp b/gui/listbox.cpp index 3386465e..9fbe0923 100644 --- a/gui/listbox.cpp +++ b/gui/listbox.cpp @@ -26,7 +26,6 @@ extern "C" { #include "rapidxml.hpp" #include "objects.hpp" #include "../data.hpp" -#include "../partitions.hpp" #include "pages.hpp" extern std::vector Language_List; @@ -83,28 +82,10 @@ GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) data.selected = 0; mListItems.push_back(data); } - } else if (mVariable == "tw_crypto_user_id") { - std::vector::iterator iter; - std::vector* Users_List = PartitionManager.Get_Users_List(); - for (iter = Users_List->begin(); iter != Users_List->end(); iter++) { - if (!(*iter).isDecrypted) { - ListItem data; - data.displayName = (*iter).userName; - data.variableValue = (*iter).userId; - data.action = NULL; - DataManager::GetValue("tw_crypto_user_id", currentValue); - if (currentValue == (*iter).userId || currentValue == "") { - data.selected = 1; - DataManager::SetValue("tw_crypto_user_id", (*iter).userId); - DataManager::SetValue("tw_crypto_pwtype", (*iter).type); - } else - data.selected = 0; - mListItems.push_back(data); - } - } } - } else - allowSelection = false; // allows using listbox as a read-only list or menu + } + else + allowSelection = false; // allows using listbox as a read-only list or menu // Get the data for the list child = FindNode(node, "listitem"); @@ -113,14 +94,16 @@ GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) ListItem item; attr = child->first_attribute("name"); - if (!attr) continue; + if (!attr) + continue; // We will parse display names when we get page focus to ensure that translating takes place item.displayName = attr->value(); item.variableValue = gui_parse_text(child->value()); item.selected = (child->value() == currentValue); item.action = NULL; xml_node<>* action = child->first_node("action"); - if (!action) action = child->first_node("actions"); + if (!action) + action = child->first_node("actions"); if (action) { item.action = new GUIAction(child); allowSelection = true; @@ -139,7 +122,7 @@ GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) LoadConditions(child, item.mConditions); mListItems.push_back(item); - mVisibleItems.push_back(mListItems.size() - 1); + mVisibleItems.push_back(mListItems.size()-1); child = child->next_sibling("listitem"); } @@ -154,33 +137,6 @@ int GUIListBox::Update(void) if (!isConditionTrue()) return 0; - if (mVariable == "tw_crypto_user_id") { - mListItems.clear(); - std::vector::iterator iter; - std::vector* Users_List = PartitionManager.Get_Users_List(); - for (iter = Users_List->begin(); iter != Users_List->end(); iter++) { - if (!(*iter).isDecrypted) { - ListItem data; - data.displayName = (*iter).userName; - data.variableValue = (*iter).userId; - data.action = NULL; - DataManager::GetValue("tw_crypto_user_id", currentValue); - if (currentValue == (*iter).userId || currentValue == "") { - data.selected = 1; - DataManager::SetValue("tw_crypto_user_id", (*iter).userId); - DataManager::SetValue("tw_crypto_pwtype", (*iter).type); - } else - data.selected = 0; - mListItems.push_back(data); - } - } - mVisibleItems.clear(); - for (size_t i = 0; i < mListItems.size(); i++) { - mVisibleItems.push_back(i); - } - mUpdate = 1; - } - GUIScrollList::Update(); if (mUpdate) { diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml index 7fc9980b..3af174ee 100755 --- a/gui/theme/common/landscape.xml +++ b/gui/theme/common/landscape.xml @@ -759,7 +759,6 @@ tw_cancel_param= tw_show_exclamation=0 tw_show_reboot=0 - tw_crypto_user_id= %tw_clear_destination% @@ -1062,14 +1061,9 @@ {@factory_reset1=Wipes Data, Cache, and Dalvik} - - - {@factory_reset5=(not including users/lockscreen)} - - - + {@factory_reset2=(not including internal storage)} @@ -2523,10 +2517,6 @@ {@decrypt_data_btn=Decrypt Data} - tw_crypto_user_id=0 - tw_crypto_password= - tw_password_fail=0 - tw_crypto_pwtype=%tw_crypto_pwtype_0% decrypt @@ -3393,16 +3383,6 @@ - - - - - - - tw_crypto_user_id= - decrypt_users - - @@ -4416,17 +4396,10 @@ - {@decrypt_data_enter_pass=Enter Password:} - - - - {@decrypt_data_enter_pass_fbe=Enter Password for User [%tw_crypto_user_id%]} - - %tw_crypto_display% @@ -5406,44 +5379,5 @@ - - -