blob: 285dae91062d27d41014569a0d67a7bea90d0576 [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>
Nikita Ioffe29e15c82023-02-25 02:31:51 +000024#include <fstab/fstab.h>
Inseob Kimdb319702022-01-20 13:12:43 +090025#include <fsverity_digests.pb.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090026#include <linux/vm_sockets.h>
27#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090028#include <stdio.h>
Nikita Ioffe3452ee22022-12-15 00:31:56 +000029#include <sys/capability.h>
Jiyong Park23934392021-06-16 01:59:10 +090030#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090031#include <unistd.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010032#include <vm_main.h>
Alan Stokesd4ea5a82022-11-10 12:17:42 +000033#include <vm_payload_restricted.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090034
Inseob Kim691df6a2022-01-20 12:54:30 +090035#include <string>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000036#include <thread>
Jiyong Parka7266ac2021-05-17 21:57:24 +090037
Alan Stokesd1a30dd2022-11-30 09:39:54 +000038using android::base::borrowed_fd;
Inseob Kim06a64d62021-09-07 21:21:45 +090039using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000040using android::base::Error;
Nikita Ioffe3452ee22022-12-15 00:31:56 +000041using android::base::make_scope_guard;
Andrew Scull11cf0902021-06-22 12:08:10 +000042using android::base::Result;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000043using android::base::unique_fd;
Nikita Ioffe29e15c82023-02-25 02:31:51 +000044using android::fs_mgr::Fstab;
45using android::fs_mgr::FstabEntry;
46using android::fs_mgr::GetEntryForMountPoint;
47using android::fs_mgr::ReadFstabFromFile;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000048
49using aidl::com::android::microdroid::testservice::BnTestService;
50using ndk::ScopedAStatus;
Andrew Scull66616612021-06-17 16:41:03 +000051
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090052extern void testlib_sub();
53
Andrew Scull66616612021-06-17 16:41:03 +000054namespace {
55
Alan Stokesd1a30dd2022-11-30 09:39:54 +000056constexpr char TAG[] = "testbinary";
57
Andrew Scull11cf0902021-06-22 12:08:10 +000058template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +000059Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +000060 auto property = "debug.microdroid.test." + name;
61 std::stringstream outcome;
62 if (result.ok()) {
63 outcome << "PASS";
64 } else {
65 outcome << "FAIL: " << result.error();
Alan Stokesd1a30dd2022-11-30 09:39:54 +000066 // Log the error in case the property is truncated.
67 std::string message = name + ": " + outcome.str();
68 __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str());
Andrew Scull11cf0902021-06-22 12:08:10 +000069 }
70 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +000071 return result;
Andrew Scull66616612021-06-17 16:41:03 +000072}
73
Alan Stokesd1a30dd2022-11-30 09:39:54 +000074Result<void> run_echo_reverse_server(borrowed_fd listening_fd) {
75 struct sockaddr_vm client_sa = {};
76 socklen_t client_sa_len = sizeof(client_sa);
77 unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len,
78 SOCK_CLOEXEC)};
79 if (!connect_fd.ok()) {
80 return ErrnoError() << "Failed to accept vsock connection";
81 }
82
83 unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)};
84 if (!input_fd.ok()) {
85 return ErrnoError() << "Failed to dup";
86 }
87 FILE* input = fdopen(input_fd.release(), "r");
88 if (!input) {
89 return ErrnoError() << "Failed to fdopen";
90 }
91
David Brazdilbcce3512023-02-23 15:32:55 +000092 // Run forever, reverse one line at a time.
93 while (true) {
94 char* line = nullptr;
95 size_t size = 0;
96 if (getline(&line, &size, input) < 0) {
97 return ErrnoError() << "Failed to read";
98 }
99
100 std::string_view original = line;
101 if (!original.empty() && original.back() == '\n') {
102 original = original.substr(0, original.size() - 1);
103 }
104
105 std::string reversed(original.rbegin(), original.rend());
106 reversed += "\n";
107
108 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
109 return ErrnoError() << "Failed to write";
110 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000111 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000112}
113
114Result<void> start_echo_reverse_server() {
115 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
116 if (!server_fd.ok()) {
117 return ErrnoError() << "Failed to create vsock socket";
118 }
119 struct sockaddr_vm server_sa = (struct sockaddr_vm){
120 .svm_family = AF_VSOCK,
Alan Stokes10c47672022-12-13 17:17:08 +0000121 .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT),
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000122 .svm_cid = VMADDR_CID_ANY,
123 };
124 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
125 if (ret < 0) {
126 return ErrnoError() << "Failed to bind vsock socket";
127 }
128 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
129 if (ret < 0) {
130 return ErrnoError() << "Failed to listen";
131 }
132
133 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
134 auto result = run_echo_reverse_server(listening_fd);
135 if (!result.ok()) {
136 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
137 // Make sure the VM exits so the test will fail solidly
138 exit(1);
139 }
140 }};
141 accept_thread.detach();
142
143 return {};
144}
145
Inseob Kim06a64d62021-09-07 21:21:45 +0900146Result<void> start_test_service() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000147 class TestService : public BnTestService {
148 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900149 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000150 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900151 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900152
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000153 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900154 *out = android::base::GetProperty(prop, "");
155 if (out->empty()) {
156 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000157 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
158 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900159 }
160
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000161 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900162 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000163
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000164 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000165 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000166 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000167 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
168 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000169 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000170 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000171
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000172 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000173 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000174 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000175 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000176 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000177 }
Andrew Scull61892082022-02-21 00:07:25 +0000178
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000179 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000180 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000181 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000182 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000183 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000184 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000185
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000186 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000187 const char* path_c = AVmPayload_getApkContentsPath();
188 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000189 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000190 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
191 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000192 *out = path_c;
193 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000194 }
Alan Stokes78d24702022-11-21 15:28:31 +0000195
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000196 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000197 const char* path_c = AVmPayload_getEncryptedStoragePath();
198 if (path_c == nullptr) {
199 out->clear();
200 } else {
201 *out = path_c;
202 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000203 return ScopedAStatus::ok();
204 }
205
Nikita Ioffe3452ee22022-12-15 00:31:56 +0000206 ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override {
207 if (out == nullptr) {
208 return ScopedAStatus::ok();
209 }
210 cap_t cap = cap_get_proc();
211 auto guard = make_scope_guard([&cap]() { cap_free(cap); });
212 for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) {
213 cap_flag_value_t value;
214 if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) {
215 return ScopedAStatus::
216 fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed");
217 }
218 if (value == CAP_SET) {
219 // Ideally we would just send back the cap_ids, but I wasn't able to find java
220 // APIs for linux capabilities, hence we transform to the human readable name
221 // here.
222 char* name = cap_to_name(cap_id);
223 out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")");
224 }
225 }
226 return ScopedAStatus::ok();
227 }
228
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000229 virtual ::ScopedAStatus runEchoReverseServer() override {
230 auto result = start_echo_reverse_server();
231 if (result.ok()) {
232 return ScopedAStatus::ok();
233 } else {
234 std::string message = result.error().message();
235 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
236 }
Alan Stokes78d24702022-11-21 15:28:31 +0000237 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000238
239 ScopedAStatus writeToFile(const std::string& content, const std::string& path) override {
240 if (!android::base::WriteStringToFile(content, path)) {
241 std::string msg = "Failed to write " + content + " to file " + path +
242 ". Errono: " + std::to_string(errno);
243 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
244 msg.c_str());
245 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000246 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 }
Shikha Panwardef7ef92023-01-06 08:35:48 +0000258
Nikita Ioffea7cb3672023-02-24 23:10:34 +0000259 ScopedAStatus getFilePermissions(const std::string& path, int32_t* out) override {
260 struct stat sb;
261 if (stat(path.c_str(), &sb) != -1) {
262 *out = sb.st_mode;
263 } else {
264 std::string msg = "stat " + path + " failed : " + std::strerror(errno);
265 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
266 msg.c_str());
267 }
268 return ScopedAStatus::ok();
269 }
270
Nikita Ioffe29e15c82023-02-25 02:31:51 +0000271 ScopedAStatus getMountFlags(const std::string& mount_point, int32_t* out) override {
272 Fstab fstab;
273 if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
274 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
275 "Failed to read /proc/mounts");
276 }
277 FstabEntry* entry = GetEntryForMountPoint(&fstab, mount_point);
278 if (entry == nullptr) {
279 std::string msg = mount_point + " not found in /proc/mounts";
280 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
281 msg.c_str());
282 }
283 *out = entry->flags;
284 return ScopedAStatus::ok();
285 }
286
Shikha Panwardef7ef92023-01-06 08:35:48 +0000287 ScopedAStatus quit() override { exit(0); }
Inseob Kim06a64d62021-09-07 21:21:45 +0900288 };
289 auto testService = ndk::SharedRefBase::make<TestService>();
290
Alan Stokes65bbb912022-11-23 09:39:34 +0000291 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Alan Stokese0945ad2022-11-24 13:29:57 +0000292 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->SERVICE_PORT, callback,
293 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900294
295 return {};
296}
297
Inseob Kimdb319702022-01-20 13:12:43 +0900298Result<void> verify_apk() {
299 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
300
301 std::string str;
302 if (!android::base::ReadFileToString(path, &str)) {
303 return ErrnoError() << "failed to read build_manifest.pb";
304 }
305
306 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
307 return Error() << "invalid build_manifest.pb";
308 }
309
310 return {};
311}
312
Andrew Scull66616612021-06-17 16:41:03 +0000313} // Anonymous namespace
314
Alan Stokes52d3c722022-10-04 17:27:13 +0100315extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000316 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900317
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000318 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900319 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900320
Inseob Kimdb319702022-01-20 13:12:43 +0900321 // Extra apks may be missing; this is not a fatal error
322 report_test("extra_apk", verify_apk());
323
Jiyong Park23934392021-06-16 01:59:10 +0900324 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000325
Inseob Kim06a64d62021-09-07 21:21:45 +0900326 if (auto res = start_test_service(); res.ok()) {
327 return 0;
328 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000329 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900330 return 1;
331 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900332}