1 /*
2  * Copyright (C) 2021 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 <string>
18 
19 #include <android-base/file.h>
20 #include <android-base/properties.h>
21 #include <android-base/strings.h>
22 #include <fs_mgr.h>
23 #include <fstab/fstab.h>
24 #include <gtest/gtest.h>
25 
26 #include "utils.h"
27 
28 // The relevant Android API levels
29 constexpr auto S_API_LEVEL = 31;
30 
31 // As required by CDD, verified boot MUST use verification algorithms as strong
32 // as current recommendations from NIST for hashing algorithms (SHA-256).
33 // https://source.android.com/compatibility/11/android-11-cdd#9_10_device_integrity
TEST(VerifiedBootTest,avbHashtreeNotUsingSha1)34 TEST(VerifiedBootTest, avbHashtreeNotUsingSha1) {
35   int first_api_level = getFirstApiLevel();
36   int vendor_api_level = getVendorApiLevel();
37   GTEST_LOG_(INFO) << "First API level is " << first_api_level;
38   GTEST_LOG_(INFO) << "Vendor API level is " << vendor_api_level;
39   if (first_api_level < S_API_LEVEL) {
40     GTEST_LOG_(INFO)
41         << "Exempt from avb hash tree test due to old starting API level";
42     return;
43   }
44 
45   // This feature name check only applies to devices that first shipped with
46   // SC or later.
47   int min_api_level = (first_api_level < vendor_api_level) ? first_api_level
48                                                            : vendor_api_level;
49   if (min_api_level >= S_API_LEVEL &&
50       !deviceSupportsFeature("android.hardware.security.model.compatible")) {
51       GTEST_SKIP()
52           << "Skipping test: FEATURE_SECURITY_MODEL_COMPATIBLE missing.";
53       return;
54   }
55 
56   android::fs_mgr::Fstab fstab;
57   ASSERT_TRUE(ReadDefaultFstab(&fstab)) << "Failed to read default fstab";
58 
59   for (const auto& entry : fstab) {
60     if (!entry.fs_mgr_flags.verify && !entry.fs_mgr_flags.avb) {
61       continue;
62     }
63 
64     if (android::base::EqualsIgnoreCase(entry.fs_type, "emmc")) {
65       GTEST_LOG_(INFO) << entry.mount_point << " has emmc fs_type, skipping"
66           << " hashtree algorithm verification";
67       continue;
68     }
69 
70     GTEST_LOG_(ERROR) << "partition enabled verity " << entry.mount_point;
71 
72     // The verity sysprop use "system" as the partition name in the system as
73     // root case.
74     std::string partition = entry.mount_point == "/"
75                                 ? "system"
76                                 : android::base::Basename(entry.mount_point);
77 
78     std::string alg_prop_name = "partition." + partition + ".verified.hash_alg";
79     std::string hash_alg = android::base::GetProperty(alg_prop_name, "");
80     ASSERT_FALSE(hash_alg.empty());
81     ASSERT_FALSE(android::base::StartsWithIgnoreCase(hash_alg, "sha1"));
82   }
83 }
84