blob: 07c8cd428c960f0ffe0141a5ec09e6c9403bc9a3 [file] [log] [blame]
Jiyong Parka7266ac2021-05-17 21:57:24 +09001/*
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 */
Alan Stokesd1a30dd2022-11-30 09:39:54 +000016
Inseob Kim06a64d62021-09-07 21:21:45 +090017#include <aidl/com/android/microdroid/testservice/BnTestService.h>
Inseob Kimdb319702022-01-20 13:12:43 +090018#include <android-base/file.h>
Inseob Kim691df6a2022-01-20 12:54:30 +090019#include <android-base/properties.h>
Andrew Scull11cf0902021-06-22 12:08:10 +000020#include <android-base/result.h>
Nikita Ioffe3452ee22022-12-15 00:31:56 +000021#include <android-base/scopeguard.h>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000022#include <android/log.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090023#include <fcntl.h>
Inseob Kimdb319702022-01-20 13:12:43 +090024#include <fsverity_digests.pb.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090025#include <linux/vm_sockets.h>
26#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090027#include <stdio.h>
Nikita Ioffe3452ee22022-12-15 00:31:56 +000028#include <sys/capability.h>
Jiyong Park23934392021-06-16 01:59:10 +090029#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090030#include <unistd.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010031#include <vm_main.h>
Alan Stokesd4ea5a82022-11-10 12:17:42 +000032#include <vm_payload_restricted.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090033
Inseob Kim691df6a2022-01-20 12:54:30 +090034#include <string>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000035#include <thread>
Jiyong Parka7266ac2021-05-17 21:57:24 +090036
Alan Stokesd1a30dd2022-11-30 09:39:54 +000037using android::base::borrowed_fd;
Inseob Kim06a64d62021-09-07 21:21:45 +090038using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000039using android::base::Error;
Nikita Ioffe3452ee22022-12-15 00:31:56 +000040using android::base::make_scope_guard;
Andrew Scull11cf0902021-06-22 12:08:10 +000041using android::base::Result;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000042using android::base::unique_fd;
43
44using aidl::com::android::microdroid::testservice::BnTestService;
45using ndk::ScopedAStatus;
Andrew Scull66616612021-06-17 16:41:03 +000046
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090047extern void testlib_sub();
48
Andrew Scull66616612021-06-17 16:41:03 +000049namespace {
50
Alan Stokesd1a30dd2022-11-30 09:39:54 +000051constexpr char TAG[] = "testbinary";
52
Andrew Scull11cf0902021-06-22 12:08:10 +000053template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +000054Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +000055 auto property = "debug.microdroid.test." + name;
56 std::stringstream outcome;
57 if (result.ok()) {
58 outcome << "PASS";
59 } else {
60 outcome << "FAIL: " << result.error();
Alan Stokesd1a30dd2022-11-30 09:39:54 +000061 // Log the error in case the property is truncated.
62 std::string message = name + ": " + outcome.str();
63 __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str());
Andrew Scull11cf0902021-06-22 12:08:10 +000064 }
65 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +000066 return result;
Andrew Scull66616612021-06-17 16:41:03 +000067}
68
Alan Stokesd1a30dd2022-11-30 09:39:54 +000069Result<void> run_echo_reverse_server(borrowed_fd listening_fd) {
70 struct sockaddr_vm client_sa = {};
71 socklen_t client_sa_len = sizeof(client_sa);
72 unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len,
73 SOCK_CLOEXEC)};
74 if (!connect_fd.ok()) {
75 return ErrnoError() << "Failed to accept vsock connection";
76 }
77
78 unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)};
79 if (!input_fd.ok()) {
80 return ErrnoError() << "Failed to dup";
81 }
82 FILE* input = fdopen(input_fd.release(), "r");
83 if (!input) {
84 return ErrnoError() << "Failed to fdopen";
85 }
86
David Brazdilbcce3512023-02-23 15:32:55 +000087 // Run forever, reverse one line at a time.
88 while (true) {
89 char* line = nullptr;
90 size_t size = 0;
91 if (getline(&line, &size, input) < 0) {
92 return ErrnoError() << "Failed to read";
93 }
94
95 std::string_view original = line;
96 if (!original.empty() && original.back() == '\n') {
97 original = original.substr(0, original.size() - 1);
98 }
99
100 std::string reversed(original.rbegin(), original.rend());
101 reversed += "\n";
102
103 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
104 return ErrnoError() << "Failed to write";
105 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000106 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000107}
108
109Result<void> start_echo_reverse_server() {
110 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
111 if (!server_fd.ok()) {
112 return ErrnoError() << "Failed to create vsock socket";
113 }
114 struct sockaddr_vm server_sa = (struct sockaddr_vm){
115 .svm_family = AF_VSOCK,
Alan Stokes10c47672022-12-13 17:17:08 +0000116 .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT),
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000117 .svm_cid = VMADDR_CID_ANY,
118 };
119 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
120 if (ret < 0) {
121 return ErrnoError() << "Failed to bind vsock socket";
122 }
123 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
124 if (ret < 0) {
125 return ErrnoError() << "Failed to listen";
126 }
127
128 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
129 auto result = run_echo_reverse_server(listening_fd);
130 if (!result.ok()) {
131 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
132 // Make sure the VM exits so the test will fail solidly
133 exit(1);
134 }
135 }};
136 accept_thread.detach();
137
138 return {};
139}
140
Inseob Kim06a64d62021-09-07 21:21:45 +0900141Result<void> start_test_service() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000142 class TestService : public BnTestService {
143 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900144 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000145 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900146 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900147
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000148 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900149 *out = android::base::GetProperty(prop, "");
150 if (out->empty()) {
151 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000152 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
153 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900154 }
155
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000156 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900157 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000158
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000159 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000160 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000161 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000162 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
163 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000164 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000165 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000166
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000167 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000168 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000169 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000170 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000171 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000172 }
Andrew Scull61892082022-02-21 00:07:25 +0000173
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000174 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000175 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000176 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000177 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000178 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000179 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000180
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000181 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000182 const char* path_c = AVmPayload_getApkContentsPath();
183 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000184 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000185 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
186 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000187 *out = path_c;
188 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000189 }
Alan Stokes78d24702022-11-21 15:28:31 +0000190
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000191 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000192 const char* path_c = AVmPayload_getEncryptedStoragePath();
193 if (path_c == nullptr) {
194 out->clear();
195 } else {
196 *out = path_c;
197 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000198 return ScopedAStatus::ok();
199 }
200
Nikita Ioffe3452ee22022-12-15 00:31:56 +0000201 ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override {
202 if (out == nullptr) {
203 return ScopedAStatus::ok();
204 }
205 cap_t cap = cap_get_proc();
206 auto guard = make_scope_guard([&cap]() { cap_free(cap); });
207 for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) {
208 cap_flag_value_t value;
209 if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) {
210 return ScopedAStatus::
211 fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed");
212 }
213 if (value == CAP_SET) {
214 // Ideally we would just send back the cap_ids, but I wasn't able to find java
215 // APIs for linux capabilities, hence we transform to the human readable name
216 // here.
217 char* name = cap_to_name(cap_id);
218 out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")");
219 }
220 }
221 return ScopedAStatus::ok();
222 }
223
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000224 virtual ::ScopedAStatus runEchoReverseServer() override {
225 auto result = start_echo_reverse_server();
226 if (result.ok()) {
227 return ScopedAStatus::ok();
228 } else {
229 std::string message = result.error().message();
230 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
231 }
Alan Stokes78d24702022-11-21 15:28:31 +0000232 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000233
234 ScopedAStatus writeToFile(const std::string& content, const std::string& path) override {
235 if (!android::base::WriteStringToFile(content, path)) {
236 std::string msg = "Failed to write " + content + " to file " + path +
237 ". Errono: " + std::to_string(errno);
238 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
239 msg.c_str());
240 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000241 return ScopedAStatus::ok();
242 }
243
244 ScopedAStatus readFromFile(const std::string& path, std::string* out) override {
245 if (!android::base::ReadFileToString(path, out)) {
246 std::string msg =
247 "Failed to read " + path + " to string. Errono: " + std::to_string(errno);
248 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
249 msg.c_str());
250 }
251 return ScopedAStatus::ok();
252 }
Shikha Panwardef7ef92023-01-06 08:35:48 +0000253
Nikita Ioffea7cb3672023-02-24 23:10:34 +0000254 ScopedAStatus getFilePermissions(const std::string& path, int32_t* out) override {
255 struct stat sb;
256 if (stat(path.c_str(), &sb) != -1) {
257 *out = sb.st_mode;
258 } else {
259 std::string msg = "stat " + path + " failed : " + std::strerror(errno);
260 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
261 msg.c_str());
262 }
263 return ScopedAStatus::ok();
264 }
265
Shikha Panwardef7ef92023-01-06 08:35:48 +0000266 ScopedAStatus quit() override { exit(0); }
Inseob Kim06a64d62021-09-07 21:21:45 +0900267 };
268 auto testService = ndk::SharedRefBase::make<TestService>();
269
Alan Stokes65bbb912022-11-23 09:39:34 +0000270 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Alan Stokese0945ad2022-11-24 13:29:57 +0000271 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->SERVICE_PORT, callback,
272 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900273
274 return {};
275}
276
Inseob Kimdb319702022-01-20 13:12:43 +0900277Result<void> verify_apk() {
278 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
279
280 std::string str;
281 if (!android::base::ReadFileToString(path, &str)) {
282 return ErrnoError() << "failed to read build_manifest.pb";
283 }
284
285 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
286 return Error() << "invalid build_manifest.pb";
287 }
288
289 return {};
290}
291
Andrew Scull66616612021-06-17 16:41:03 +0000292} // Anonymous namespace
293
Alan Stokes52d3c722022-10-04 17:27:13 +0100294extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000295 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900296
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000297 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900298 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900299
Inseob Kimdb319702022-01-20 13:12:43 +0900300 // Extra apks may be missing; this is not a fatal error
301 report_test("extra_apk", verify_apk());
302
Jiyong Park23934392021-06-16 01:59:10 +0900303 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000304
Inseob Kim06a64d62021-09-07 21:21:45 +0900305 if (auto res = start_test_service(); res.ok()) {
306 return 0;
307 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000308 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900309 return 1;
310 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900311}