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 --- tests/component/update_verifier_test.cpp | 102 ++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 15 deletions(-) (limited to 'tests/component/update_verifier_test.cpp') diff --git a/tests/component/update_verifier_test.cpp b/tests/component/update_verifier_test.cpp index 543e4278e..2420c27fe 100644 --- a/tests/component/update_verifier_test.cpp +++ b/tests/component/update_verifier_test.cpp @@ -16,6 +16,7 @@ #include +#include #include #include #include @@ -37,8 +38,19 @@ class UpdateVerifierTest : public ::testing::Test { std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing"); + care_map_prefix_ = care_map_dir_.path + "/care_map"s; care_map_pb_ = care_map_dir_.path + "/care_map.pb"s; care_map_txt_ = care_map_dir_.path + "/care_map.txt"s; + // Overrides the the care_map_prefix. + verifier_.set_care_map_prefix(care_map_prefix_); + + property_id_ = "ro.build.fingerprint"; + fingerprint_ = android::base::GetProperty(property_id_, ""); + // Overrides the property_reader if we cannot read the given property on the device. + if (fingerprint_.empty()) { + fingerprint_ = "mock_fingerprint"; + verifier_.set_property_reader([](const std::string& /* id */) { return "mock_fingerprint"; }); + } } void TearDown() override { @@ -58,6 +70,9 @@ class UpdateVerifierTest : public ::testing::Test { if (partition.find("ranges") != partition.end()) { info.set_ranges(partition.at("ranges")); } + if (partition.find("id") != partition.end()) { + info.set_id(partition.at("id")); + } if (partition.find("fingerprint") != partition.end()) { info.set_fingerprint(partition.at("fingerprint")); } @@ -72,13 +87,16 @@ class UpdateVerifierTest : public ::testing::Test { UpdateVerifier verifier_; TemporaryDir care_map_dir_; + std::string care_map_prefix_; std::string care_map_pb_; std::string care_map_txt_; + + std::string property_id_; + std::string fingerprint_; }; TEST_F(UpdateVerifierTest, verify_image_no_care_map) { - // Non-existing care_map is allowed. - ASSERT_FALSE(verifier_.ParseCareMap("/doesntexist")); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_smoke) { @@ -90,26 +108,26 @@ TEST_F(UpdateVerifierTest, verify_image_smoke) { std::string content = "system\n2,0,1"; ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); - ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_TRUE(verifier_.ParseCareMap()); ASSERT_TRUE(verifier_.VerifyPartitions()); // Leading and trailing newlines should be accepted. ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_txt_)); - ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_TRUE(verifier_.ParseCareMap()); ASSERT_TRUE(verifier_.VerifyPartitions()); } TEST_F(UpdateVerifierTest, verify_image_empty_care_map) { - ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { // The care map file can have only 2 / 4 / 6 lines. ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_FALSE(verifier_.ParseCareMap()); ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) { @@ -121,7 +139,7 @@ TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) { std::string content = "system\n2,1,0"; ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) { @@ -133,7 +151,7 @@ TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) { std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0"; ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) { @@ -144,12 +162,17 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) { } std::vector> partitions = { - { { "name", "system" }, { "ranges", "2,0,1" } }, + { + { "name", "system" }, + { "ranges", "2,0,1" }, + { "id", property_id_ }, + { "fingerprint", fingerprint_ }, + }, }; std::string proto = ConstructProto(partitions); ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); - ASSERT_TRUE(verifier_.ParseCareMap(care_map_pb_)); + ASSERT_TRUE(verifier_.ParseCareMap()); ASSERT_TRUE(verifier_.VerifyPartitions()); } @@ -161,12 +184,16 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) { } std::vector> partitions = { - { { "ranges", "2,0,1" } }, + { + { "ranges", "2,0,1" }, + { "id", property_id_ }, + { "fingerprint", fingerprint_ }, + }, }; std::string proto = ConstructProto(partitions); ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) { @@ -177,10 +204,55 @@ TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) { } std::vector> partitions = { - { { "name", "system" }, { "ranges", "3,0,1" } }, + { + { "name", "system" }, + { "ranges", "3,0,1" }, + { "id", property_id_ }, + { "fingerprint", fingerprint_ }, + }, + }; + + std::string proto = ConstructProto(partitions); + ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); + ASSERT_FALSE(verifier_.ParseCareMap()); +} + +TEST_F(UpdateVerifierTest, verify_image_protobuf_empty_fingerprint) { + // This test relies on dm-verity support. + if (!verity_supported) { + GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; + return; + } + + std::vector> partitions = { + { + { "name", "system" }, + { "ranges", "2,0,1" }, + }, + }; + + std::string proto = ConstructProto(partitions); + ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); + ASSERT_FALSE(verifier_.ParseCareMap()); +} + +TEST_F(UpdateVerifierTest, verify_image_protobuf_fingerprint_mismatch) { + // This test relies on dm-verity support. + if (!verity_supported) { + GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; + return; + } + + std::vector> partitions = { + { + { "name", "system" }, + { "ranges", "2,0,1" }, + { "id", property_id_ }, + { "fingerprint", "unsupported_fingerprint" }, + }, }; std::string proto = ConstructProto(partitions); ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); - ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_)); + ASSERT_FALSE(verifier_.ParseCareMap()); } -- cgit v1.2.3