From 9eed65e1db49abe44daedc2f569acff6ce861035 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 14 Sep 2018 12:52:02 -0700 Subject: Compare the fingerprint before reading the partition The update_verifier now compares the fingerprint of a partition before performing the blocks read. If the fingerprint of the current system property mismatches the one embedded in the care_map, verification of this partition will be skipped. This is useful for the possible system only updates in the future. Bug: 114778109 Test: unit tests pass Change-Id: Iea309148a05109b5810dfb533d94260d77ab8540 --- .../include/update_verifier/update_verifier.h | 20 +++++++-- update_verifier/update_verifier.cpp | 47 +++++++++++++++++----- 2 files changed, 53 insertions(+), 14 deletions(-) (limited to 'update_verifier') diff --git a/update_verifier/include/update_verifier/update_verifier.h b/update_verifier/include/update_verifier/update_verifier.h index bdfabed0c..b00890e82 100644 --- a/update_verifier/include/update_verifier/update_verifier.h +++ b/update_verifier/include/update_verifier/update_verifier.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include @@ -37,15 +38,18 @@ int update_verifier(int argc, char** argv); // it should skip the verification to avoid bricking the device. class UpdateVerifier { public: + UpdateVerifier(); + // This function tries to process the care_map.pb as protobuf message; and falls back to use - // care_map.txt if the pb format file doesn't exist. If the parsing succeeds, put the result of - // the pair into the |partition_map_|. - bool ParseCareMap(const std::string& file_name = ""); + // care_map.txt if the pb format file doesn't exist. If the parsing succeeds, put the result + // of the pair into the |partition_map_|. + bool ParseCareMap(); // Verifies the new boot by reading all the cared blocks for partitions in |partition_map_|. bool VerifyPartitions(); private: + friend class UpdateVerifierTest; // Parses the legacy care_map.txt in plain text format. bool ParseCareMapPlainText(const std::string& content); @@ -56,5 +60,15 @@ class UpdateVerifier { bool ReadBlocks(const std::string partition_name, const std::string& dm_block_device, const RangeSet& ranges); + // Functions to override the care_map_prefix_ and property_reader_, used in test only. + void set_care_map_prefix(const std::string& prefix); + void set_property_reader(const std::function& property_reader); + std::map partition_map_; + // The path to the care_map excluding the filename extension; default value: + // "/data/ota_package/care_map" + std::string care_map_prefix_; + + // The function to read the device property; default value: android::base::GetProperty() + std::function property_reader_; }; diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp index 6e86ac9d3..d7cd061e2 100644 --- a/update_verifier/update_verifier.cpp +++ b/update_verifier/update_verifier.cpp @@ -65,6 +65,8 @@ using android::hardware::boot::V1_0::IBootControl; using android::hardware::boot::V1_0::BoolResult; using android::hardware::boot::V1_0::CommandResult; +constexpr const char* kDefaultCareMapPrefix = "/data/ota_package/care_map"; + // Find directories in format of "/sys/block/dm-X". static int dm_name_filter(const dirent* de) { if (android::base::StartsWith(de->d_name, "dm-")) { @@ -73,6 +75,10 @@ static int dm_name_filter(const dirent* de) { return 0; } +UpdateVerifier::UpdateVerifier() + : care_map_prefix_(kDefaultCareMapPrefix), + property_reader_([](const std::string& id) { return android::base::GetProperty(id, ""); }) {} + // Iterate the content of "/sys/block/dm-X/dm/name" and find all the dm-wrapped block devices. // We will later read all the ("cared") blocks from "/dev/block/dm-X" to ensure the target // partition's integrity. @@ -224,18 +230,14 @@ bool UpdateVerifier::ParseCareMapPlainText(const std::string& content) { return true; } -bool UpdateVerifier::ParseCareMap(const std::string& file_name) { +bool UpdateVerifier::ParseCareMap() { partition_map_.clear(); - std::string care_map_name = file_name; - if (care_map_name.empty()) { - std::string care_map_prefix = "/data/ota_package/care_map"; - care_map_name = care_map_prefix + ".pb"; - if (access(care_map_name.c_str(), R_OK) == -1) { - LOG(WARNING) << care_map_name - << " doesn't exist, falling back to read the care_map in plain text format."; - care_map_name = care_map_prefix + ".txt"; - } + std::string care_map_name = care_map_prefix_ + ".pb"; + if (access(care_map_name.c_str(), R_OK) == -1) { + LOG(WARNING) << care_map_name + << " doesn't exist, falling back to read the care_map in plain text format."; + care_map_name = care_map_prefix_ + ".txt"; } android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); @@ -283,7 +285,21 @@ bool UpdateVerifier::ParseCareMap(const std::string& file_name) { return false; } - // TODO(xunchang) compare the fingerprint of the partition. + // Continues to check other partitions if there is a fingerprint mismatch. + if (partition.id().empty() || partition.id() == "unknown") { + LOG(WARNING) << "Skip reading partition " << partition.name() + << ": property_id is not provided to get fingerprint."; + continue; + } + + std::string fingerprint = property_reader_(partition.id()); + if (fingerprint != partition.fingerprint()) { + LOG(WARNING) << "Skip reading partition " << partition.name() << ": fingerprint " + << fingerprint << " doesn't match the expected value " + << partition.fingerprint(); + continue; + } + partition_map_.emplace(partition.name(), ranges); } @@ -295,6 +311,15 @@ bool UpdateVerifier::ParseCareMap(const std::string& file_name) { return true; } +void UpdateVerifier::set_care_map_prefix(const std::string& prefix) { + care_map_prefix_ = prefix; +} + +void UpdateVerifier::set_property_reader( + const std::function& property_reader) { + property_reader_ = property_reader; +} + static int reboot_device() { if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) { LOG(ERROR) << "Failed to reboot."; -- cgit v1.2.3