blob: 4ba502a0eb3056bb84eabc9bc87331a65a301808 [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
87 char* line = nullptr;
88 size_t size = 0;
89 if (getline(&line, &size, input) < 0) {
90 return ErrnoError() << "Failed to read";
91 }
92
93 if (fclose(input) != 0) {
94 return ErrnoError() << "Failed to fclose";
95 }
96
97 std::string_view original = line;
98 if (!original.empty() && original.back() == '\n') {
99 original = original.substr(0, original.size() - 1);
100 }
101
102 std::string reversed(original.rbegin(), original.rend());
103
104 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
105 return ErrnoError() << "Failed to write";
106 }
107
108 return {};
109}
110
111Result<void> start_echo_reverse_server() {
112 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
113 if (!server_fd.ok()) {
114 return ErrnoError() << "Failed to create vsock socket";
115 }
116 struct sockaddr_vm server_sa = (struct sockaddr_vm){
117 .svm_family = AF_VSOCK,
Alan Stokes10c47672022-12-13 17:17:08 +0000118 .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT),
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000119 .svm_cid = VMADDR_CID_ANY,
120 };
121 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
122 if (ret < 0) {
123 return ErrnoError() << "Failed to bind vsock socket";
124 }
125 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
126 if (ret < 0) {
127 return ErrnoError() << "Failed to listen";
128 }
129
130 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
131 auto result = run_echo_reverse_server(listening_fd);
132 if (!result.ok()) {
133 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
134 // Make sure the VM exits so the test will fail solidly
135 exit(1);
136 }
137 }};
138 accept_thread.detach();
139
140 return {};
141}
142
Inseob Kim06a64d62021-09-07 21:21:45 +0900143Result<void> start_test_service() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000144 class TestService : public BnTestService {
145 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900146 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000147 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900148 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900149
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000150 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900151 *out = android::base::GetProperty(prop, "");
152 if (out->empty()) {
153 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000154 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
155 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900156 }
157
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000158 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900159 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000160
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000161 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000162 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000163 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000164 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
165 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000166 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000167 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000168
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000169 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000170 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000171 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000172 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000173 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000174 }
Andrew Scull61892082022-02-21 00:07:25 +0000175
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000176 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000177 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000178 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000179 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000180 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000181 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000182
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000183 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000184 const char* path_c = AVmPayload_getApkContentsPath();
185 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000186 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000187 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
188 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000189 *out = path_c;
190 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000191 }
Alan Stokes78d24702022-11-21 15:28:31 +0000192
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000193 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000194 const char* path_c = AVmPayload_getEncryptedStoragePath();
195 if (path_c == nullptr) {
196 out->clear();
197 } else {
198 *out = path_c;
199 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000200 return ScopedAStatus::ok();
201 }
202
Nikita Ioffe3452ee22022-12-15 00:31:56 +0000203 ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override {
204 if (out == nullptr) {
205 return ScopedAStatus::ok();
206 }
207 cap_t cap = cap_get_proc();
208 auto guard = make_scope_guard([&cap]() { cap_free(cap); });
209 for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) {
210 cap_flag_value_t value;
211 if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) {
212 return ScopedAStatus::
213 fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed");
214 }
215 if (value == CAP_SET) {
216 // Ideally we would just send back the cap_ids, but I wasn't able to find java
217 // APIs for linux capabilities, hence we transform to the human readable name
218 // here.
219 char* name = cap_to_name(cap_id);
220 out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")");
221 }
222 }
223 return ScopedAStatus::ok();
224 }
225
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000226 virtual ::ScopedAStatus runEchoReverseServer() override {
227 auto result = start_echo_reverse_server();
228 if (result.ok()) {
229 return ScopedAStatus::ok();
230 } else {
231 std::string message = result.error().message();
232 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
233 }
Alan Stokes78d24702022-11-21 15:28:31 +0000234 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000235
236 ScopedAStatus writeToFile(const std::string& content, const std::string& path) override {
237 if (!android::base::WriteStringToFile(content, path)) {
238 std::string msg = "Failed to write " + content + " to file " + path +
239 ". Errono: " + std::to_string(errno);
240 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
241 msg.c_str());
242 }
243 // TODO(b/264520098): Remove sync() once TestService supports quit() method
244 // and Microdroid manager flushes filesystem caches on shutdown.
245 sync();
246 return ScopedAStatus::ok();
247 }
248
249 ScopedAStatus readFromFile(const std::string& path, std::string* out) override {
250 if (!android::base::ReadFileToString(path, out)) {
251 std::string msg =
252 "Failed to read " + path + " to string. Errono: " + std::to_string(errno);
253 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
254 msg.c_str());
255 }
256 return ScopedAStatus::ok();
257 }
Inseob Kim06a64d62021-09-07 21:21:45 +0900258 };
259 auto testService = ndk::SharedRefBase::make<TestService>();
260
Alan Stokes65bbb912022-11-23 09:39:34 +0000261 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Alan Stokese0945ad2022-11-24 13:29:57 +0000262 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->SERVICE_PORT, callback,
263 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900264
265 return {};
266}
267
Inseob Kimdb319702022-01-20 13:12:43 +0900268Result<void> verify_apk() {
269 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
270
271 std::string str;
272 if (!android::base::ReadFileToString(path, &str)) {
273 return ErrnoError() << "failed to read build_manifest.pb";
274 }
275
276 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
277 return Error() << "invalid build_manifest.pb";
278 }
279
280 return {};
281}
282
Andrew Scull66616612021-06-17 16:41:03 +0000283} // Anonymous namespace
284
Alan Stokes52d3c722022-10-04 17:27:13 +0100285extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000286 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900287
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000288 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900289 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900290
Inseob Kimdb319702022-01-20 13:12:43 +0900291 // Extra apks may be missing; this is not a fatal error
292 report_test("extra_apk", verify_apk());
293
Jiyong Park23934392021-06-16 01:59:10 +0900294 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000295
Inseob Kim06a64d62021-09-07 21:21:45 +0900296 if (auto res = start_test_service(); res.ok()) {
297 return 0;
298 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000299 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900300 return 1;
301 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900302}