• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <arpa/inet.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <sys/un.h>
21 #include <unistd.h>
22 
23 #include <algorithm>
24 #include <string>
25 
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/properties.h>
29 #include <android-base/test_utils.h>
30 #include <android-base/unique_fd.h>
31 #include <bootloader_message/bootloader_message.h>
32 #include <gtest/gtest.h>
33 
34 using namespace std::string_literals;
35 
36 static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
37 static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
38 static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
39 static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt";
40 static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30;
41 
StopService()42 static void StopService() {
43   ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
44   ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
45   ASSERT_TRUE(android::base::SetProperty("ctl.stop", "uncrypt"));
46 
47   bool success = false;
48   for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
49     std::string setup_bcb = android::base::GetProperty(INIT_SVC_SETUP_BCB, "");
50     std::string clear_bcb = android::base::GetProperty(INIT_SVC_CLEAR_BCB, "");
51     std::string uncrypt = android::base::GetProperty(INIT_SVC_UNCRYPT, "");
52     GTEST_LOG_(INFO) << "setup-bcb: [" << setup_bcb << "] clear-bcb: [" << clear_bcb
53                      << "] uncrypt: [" << uncrypt << "]";
54     if (setup_bcb != "running" && clear_bcb != "running" && uncrypt != "running") {
55       success = true;
56       break;
57     }
58     sleep(1);
59   }
60 
61   ASSERT_TRUE(success) << "uncrypt service is not available.";
62 }
63 
64 class UncryptTest : public ::testing::Test {
65  protected:
UncryptTest()66   UncryptTest() : has_misc(true) {}
67 
SetUp()68   void SetUp() override {
69     std::string err;
70     has_misc = !get_bootloader_message_blk_device(&err).empty();
71   }
72 
TearDown()73   void TearDown() override {
74     // Clear the BCB.
75     if (has_misc) {
76       std::string err;
77       ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
78     }
79   }
80 
SetupOrClearBcb(bool isSetup,const std::string & message,const std::string & message_in_bcb) const81   void SetupOrClearBcb(bool isSetup, const std::string& message,
82                        const std::string& message_in_bcb) const {
83     // Restart the setup-bcb service.
84     StopService();
85     ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
86 
87     // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
88     sleep(1);
89 
90     sockaddr_un un = {};
91     un.sun_family = AF_UNIX;
92     strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
93 
94     int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
95     ASSERT_NE(-1, sockfd);
96 
97     // Connect to the uncrypt socket.
98     bool success = false;
99     for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
100       if (connect(sockfd, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
101         success = true;
102         break;
103       }
104       sleep(1);
105     }
106     ASSERT_TRUE(success);
107 
108     if (isSetup) {
109       // Send out the BCB message.
110       int length = static_cast<int>(message.size());
111       int length_out = htonl(length);
112       ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
113           << "Failed to write length: " << strerror(errno);
114       ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
115           << "Failed to write message: " << strerror(errno);
116     }
117 
118     // Check the status code from uncrypt.
119     int status;
120     ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
121     ASSERT_EQ(100U, ntohl(status));
122 
123     // Ack having received the status code.
124     int code = 0;
125     ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
126 
127     ASSERT_EQ(0, close(sockfd));
128 
129     ASSERT_TRUE(android::base::SetProperty("ctl.stop", isSetup ? "setup-bcb" : "clear-bcb"));
130 
131     // Verify the message by reading from BCB directly.
132     bootloader_message boot;
133     std::string err;
134     ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
135 
136     if (isSetup) {
137       ASSERT_EQ("boot-recovery", std::string(boot.command));
138       ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
139 
140       // The rest of the boot.recovery message should be zero'd out.
141       ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
142       size_t left = sizeof(boot.recovery) - message_in_bcb.size();
143       ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
144 
145       // Clear the BCB.
146       ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
147     } else {
148       // All the bytes should be cleared.
149       ASSERT_EQ(std::string(sizeof(boot), '\0'),
150                 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
151     }
152   }
153 
VerifyBootloaderMessage(const std::string & expected)154   void VerifyBootloaderMessage(const std::string& expected) {
155     std::string err;
156     bootloader_message boot;
157     ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
158 
159     // Check that we have all the expected bytes.
160     ASSERT_EQ(expected, std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
161   }
162 
163   bool has_misc;
164 };
165 
TEST_F(UncryptTest,setup_bcb)166 TEST_F(UncryptTest, setup_bcb) {
167   if (!has_misc) {
168     GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
169     return;
170   }
171 
172   std::string random_data;
173   random_data.reserve(sizeof(bootloader_message));
174   generate_n(back_inserter(random_data), sizeof(bootloader_message), []() { return rand() % 128; });
175 
176   bootloader_message boot;
177   memcpy(&boot, random_data.c_str(), random_data.size());
178 
179   std::string err;
180   ASSERT_TRUE(write_bootloader_message(boot, &err)) << "Failed to write BCB: " << err;
181   VerifyBootloaderMessage(random_data);
182 
183   ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
184   VerifyBootloaderMessage(std::string(sizeof(bootloader_message), '\0'));
185 
186   std::string message = "--update_message=abc value";
187   std::string message_in_bcb = "recovery\n--update_message=abc value\n";
188   SetupOrClearBcb(true, message, message_in_bcb);
189 
190   SetupOrClearBcb(false, "", "");
191 
192   TemporaryFile wipe_package;
193   ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
194 
195   // It's expected to store a wipe package in /misc, with the package size passed to recovery.
196   message = "--wipe_ab\n--wipe_package="s + wipe_package.path + "\n--reason=wipePackage"s;
197   message_in_bcb = "recovery\n--wipe_ab\n--wipe_package_size=345\n--reason=wipePackage\n";
198   SetupOrClearBcb(true, message, message_in_bcb);
199 }
200