blob: 8a0019d25facc6f044d02ed37f9f4e42b1102f21 [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>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000021#include <android/log.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090022#include <fcntl.h>
Inseob Kimdb319702022-01-20 13:12:43 +090023#include <fsverity_digests.pb.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090024#include <linux/vm_sockets.h>
25#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090026#include <stdio.h>
Jiyong Park23934392021-06-16 01:59:10 +090027#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090028#include <unistd.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010029#include <vm_main.h>
Alan Stokesd4ea5a82022-11-10 12:17:42 +000030#include <vm_payload_restricted.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090031
Inseob Kim691df6a2022-01-20 12:54:30 +090032#include <string>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000033#include <thread>
Jiyong Parka7266ac2021-05-17 21:57:24 +090034
Alan Stokesd1a30dd2022-11-30 09:39:54 +000035using android::base::borrowed_fd;
Inseob Kim06a64d62021-09-07 21:21:45 +090036using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000037using android::base::Error;
38using android::base::Result;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000039using android::base::unique_fd;
40
41using aidl::com::android::microdroid::testservice::BnTestService;
42using ndk::ScopedAStatus;
Andrew Scull66616612021-06-17 16:41:03 +000043
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090044extern void testlib_sub();
45
Andrew Scull66616612021-06-17 16:41:03 +000046namespace {
47
Alan Stokesd1a30dd2022-11-30 09:39:54 +000048constexpr char TAG[] = "testbinary";
49
Andrew Scull11cf0902021-06-22 12:08:10 +000050template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +000051Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +000052 auto property = "debug.microdroid.test." + name;
53 std::stringstream outcome;
54 if (result.ok()) {
55 outcome << "PASS";
56 } else {
57 outcome << "FAIL: " << result.error();
Alan Stokesd1a30dd2022-11-30 09:39:54 +000058 // Log the error in case the property is truncated.
59 std::string message = name + ": " + outcome.str();
60 __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str());
Andrew Scull11cf0902021-06-22 12:08:10 +000061 }
62 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +000063 return result;
Andrew Scull66616612021-06-17 16:41:03 +000064}
65
Alan Stokesd1a30dd2022-11-30 09:39:54 +000066Result<void> run_echo_reverse_server(borrowed_fd listening_fd) {
67 struct sockaddr_vm client_sa = {};
68 socklen_t client_sa_len = sizeof(client_sa);
69 unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len,
70 SOCK_CLOEXEC)};
71 if (!connect_fd.ok()) {
72 return ErrnoError() << "Failed to accept vsock connection";
73 }
74
75 unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)};
76 if (!input_fd.ok()) {
77 return ErrnoError() << "Failed to dup";
78 }
79 FILE* input = fdopen(input_fd.release(), "r");
80 if (!input) {
81 return ErrnoError() << "Failed to fdopen";
82 }
83
84 char* line = nullptr;
85 size_t size = 0;
86 if (getline(&line, &size, input) < 0) {
87 return ErrnoError() << "Failed to read";
88 }
89
90 if (fclose(input) != 0) {
91 return ErrnoError() << "Failed to fclose";
92 }
93
94 std::string_view original = line;
95 if (!original.empty() && original.back() == '\n') {
96 original = original.substr(0, original.size() - 1);
97 }
98
99 std::string reversed(original.rbegin(), original.rend());
100
101 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
102 return ErrnoError() << "Failed to write";
103 }
104
105 return {};
106}
107
108Result<void> start_echo_reverse_server() {
109 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
110 if (!server_fd.ok()) {
111 return ErrnoError() << "Failed to create vsock socket";
112 }
113 struct sockaddr_vm server_sa = (struct sockaddr_vm){
114 .svm_family = AF_VSOCK,
115 .svm_port = BnTestService::ECHO_REVERSE_PORT,
116 .svm_cid = VMADDR_CID_ANY,
117 };
118 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
119 if (ret < 0) {
120 return ErrnoError() << "Failed to bind vsock socket";
121 }
122 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
123 if (ret < 0) {
124 return ErrnoError() << "Failed to listen";
125 }
126
127 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
128 auto result = run_echo_reverse_server(listening_fd);
129 if (!result.ok()) {
130 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
131 // Make sure the VM exits so the test will fail solidly
132 exit(1);
133 }
134 }};
135 accept_thread.detach();
136
137 return {};
138}
139
Inseob Kim06a64d62021-09-07 21:21:45 +0900140Result<void> start_test_service() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000141 class TestService : public BnTestService {
142 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900143 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000144 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900145 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900146
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000147 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900148 *out = android::base::GetProperty(prop, "");
149 if (out->empty()) {
150 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000151 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
152 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900153 }
154
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000155 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900156 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000157
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000158 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000159 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000160 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000161 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
162 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000163 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000164 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000165
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000166 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000167 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000168 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000169 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000170 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000171 }
Andrew Scull61892082022-02-21 00:07:25 +0000172
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000173 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000174 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000175 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000176 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000177 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000178 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000179
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000180 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000181 const char* path_c = AVmPayload_getApkContentsPath();
182 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000183 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000184 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
185 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000186 *out = path_c;
187 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000188 }
Alan Stokes78d24702022-11-21 15:28:31 +0000189
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000190 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000191 const char* path_c = AVmPayload_getEncryptedStoragePath();
192 if (path_c == nullptr) {
193 out->clear();
194 } else {
195 *out = path_c;
196 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000197 return ScopedAStatus::ok();
198 }
199
200 virtual ::ScopedAStatus runEchoReverseServer() override {
201 auto result = start_echo_reverse_server();
202 if (result.ok()) {
203 return ScopedAStatus::ok();
204 } else {
205 std::string message = result.error().message();
206 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
207 }
Alan Stokes78d24702022-11-21 15:28:31 +0000208 }
Inseob Kim06a64d62021-09-07 21:21:45 +0900209 };
210 auto testService = ndk::SharedRefBase::make<TestService>();
211
Alan Stokes65bbb912022-11-23 09:39:34 +0000212 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Alan Stokese0945ad2022-11-24 13:29:57 +0000213 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->SERVICE_PORT, callback,
214 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900215
216 return {};
217}
218
Inseob Kimdb319702022-01-20 13:12:43 +0900219Result<void> verify_apk() {
220 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
221
222 std::string str;
223 if (!android::base::ReadFileToString(path, &str)) {
224 return ErrnoError() << "failed to read build_manifest.pb";
225 }
226
227 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
228 return Error() << "invalid build_manifest.pb";
229 }
230
231 return {};
232}
233
Andrew Scull66616612021-06-17 16:41:03 +0000234} // Anonymous namespace
235
Alan Stokes52d3c722022-10-04 17:27:13 +0100236extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000237 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900238
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000239 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900240 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900241
Inseob Kimdb319702022-01-20 13:12:43 +0900242 // Extra apks may be missing; this is not a fatal error
243 report_test("extra_apk", verify_apk());
244
Jiyong Park23934392021-06-16 01:59:10 +0900245 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000246
Inseob Kim06a64d62021-09-07 21:21:45 +0900247 if (auto res = start_test_service(); res.ok()) {
248 return 0;
249 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000250 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900251 return 1;
252 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900253}