From 37c3aef4e8447463907f8d7e7c254951b1a022a2 Mon Sep 17 00:00:00 2001 From: Captain Throwback Date: Wed, 6 Oct 2021 11:38:18 -0400 Subject: [PATCH] crypto: only set crypto state and type if not already set Move setting crypto state and type to functions and call those functions rather than arbitrarily setting those props in every case. This cleans up all the log spam from trying to set read-only props, since they will now only get set if they aren't already. Change-Id: I392bee060d71c6ee50d0d92bf1b118d9049be41a --- partition.cpp | 6 +++--- partitionmanager.cpp | 30 +++++++++++++++++++----------- partitions.hpp | 3 ++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/partition.cpp b/partition.cpp index e0c32b1f..5af68c98 100755 --- a/partition.cpp +++ b/partition.cpp @@ -737,8 +737,8 @@ bool TWPartition::Decrypt_FBE_DE() { if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { DataManager::SetValue(TW_IS_FBE, 1); DataManager::SetValue(TW_CRYPTO_PWTYPE, "0"); // Set initial value so that recovery will not be confused when using unencrypted data or failed to decrypt data - property_set("ro.crypto.state", "encrypted"); - property_set("ro.crypto.type", "file"); + PartitionManager.Set_Crypto_State(); + PartitionManager.Set_Crypto_Type("file"); LOGINFO("File Based Encryption is present\n"); #ifdef TW_INCLUDE_FBE Is_FBE = true; @@ -765,7 +765,7 @@ bool TWPartition::Decrypt_FBE_DE() { while (!Decrypt_DE() && --retry_count) usleep(2000); if (retry_count > 0) { - property_set("ro.crypto.state", "encrypted"); + PartitionManager.Set_Crypto_State(); Is_Encrypted = true; Is_Decrypted = false; DataManager::SetValue(TW_IS_ENCRYPTED, 1); diff --git a/partitionmanager.cpp b/partitionmanager.cpp index 4bd7a7dd..0b3881ef 100755 --- a/partitionmanager.cpp +++ b/partitionmanager.cpp @@ -140,9 +140,18 @@ TWPartitionManager::TWPartitionManager(void) { #endif } -int TWPartitionManager::Set_FDE_Encrypt_Status(void) { - property_set("ro.crypto.state", "encrypted"); - property_set("ro.crypto.type", "block"); +void TWPartitionManager::Set_Crypto_State() { + char crypto_state[PROPERTY_VALUE_MAX]; + property_get("ro.crypto.state", crypto_state, "error"); + if (strcmp(crypto_state, "error") == 0) + property_set("ro.crypto.state", "encrypted"); +} + +int TWPartitionManager::Set_Crypto_Type(const char* crypto_type) { + char type_prop[PROPERTY_VALUE_MAX]; + property_get("ro.crypto.type", type_prop, "error"); + if (strcmp(type_prop, "error") == 0) + property_set("ro.crypto.type", crypto_type); // Sleep for a bit so that services can start if needed sleep(1); return 0; @@ -410,9 +419,9 @@ void TWPartitionManager::Decrypt_Data() { #ifdef TW_INCLUDE_CRYPTO TWPartition* Decrypt_Data = Find_Partition_By_Path("/data"); if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) { - property_set("ro.crypto.state", "encrypted"); + Set_Crypto_State(); if (!Decrypt_Data->Key_Directory.empty() && Mount_By_Path(Decrypt_Data->Key_Directory, false)) { - property_set("ro.crypto.type", "file"); + Set_Crypto_Type("file"); #ifdef TW_INCLUDE_FBE_METADATA_DECRYPT #ifdef USE_FSCRYPT if (fscrypt_mount_metadata_encrypted(Decrypt_Data->Actual_Block_Device, Decrypt_Data->Mount_Point, false)) { @@ -450,7 +459,8 @@ void TWPartitionManager::Decrypt_Data() { } } else { LOGINFO("FBE setup failed. Trying FDE..."); - Set_FDE_Encrypt_Status(); + Set_Crypto_State(); + Set_Crypto_Type("block"); int password_type = cryptfs_get_password_type(); if (password_type == CRYPT_TYPE_DEFAULT) { LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n"); @@ -1899,7 +1909,7 @@ void TWPartitionManager::Check_Users_Decryption_Status() { int TWPartitionManager::Decrypt_Device(string Password, int user_id) { #ifdef TW_INCLUDE_CRYPTO - char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX]; + char crypto_blkdev[PROPERTY_VALUE_MAX]; std::vector::iterator iter; // Mount any partitions that need to be mounted for decrypt @@ -1910,10 +1920,8 @@ int TWPartitionManager::Decrypt_Device(string Password, int user_id) { } property_set("twrp.mount_to_decrypt", "1"); - property_get("ro.crypto.state", crypto_state, "error"); - if (strcmp(crypto_state, "error") == 0) { - Set_FDE_Encrypt_Status(); - } + Set_Crypto_State(); + Set_Crypto_Type("block"); if (DataManager::GetIntValue(TW_IS_FBE)) { #ifdef TW_INCLUDE_FBE diff --git a/partitions.hpp b/partitions.hpp index 8054734a..1b2f3cbf 100755 --- a/partitions.hpp +++ b/partitions.hpp @@ -400,7 +400,8 @@ public: void Setup_Super_Partition(); // Setup the super partition for backup and restore bool Recreate_Logs_Dir(); // Recreate TWRP_AB_LOGS_DIR after wipe std::vector* Get_Users_List(); // Returns pointer to list of users - int Set_FDE_Encrypt_Status(); // Sets encryption state for FDE devices (ro.crypto.state and ro.crypto.type) + void Set_Crypto_State(); // Sets encryption state for devices (ro.crypto.state) + int Set_Crypto_Type(const char* crypto_type); // Sets encryption type for FDE (block) and FBE (file) devices (ro.crypto.type) void Unlock_Block_Partitions(); // Unlock all block devices after update_engine runs bool Unmap_Super_Devices(); // Unmap super devices in TWRP bool Check_Pending_Merges(); // Check and run pending merges on data for VAB devices