Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 1 | /* |
| 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 Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 16 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 17 | #include <aidl/com/android/microdroid/testservice/BnTestService.h> |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 18 | #include <aidl/com/android/microdroid/testservice/BnVmCallback.h> |
| 19 | #include <aidl/com/android/microdroid/testservice/IAppCallback.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 20 | #include <android-base/file.h> |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 22 | #include <android-base/result.h> |
Nikita Ioffe | 3452ee2 | 2022-12-15 00:31:56 +0000 | [diff] [blame] | 23 | #include <android-base/scopeguard.h> |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 24 | #include <android/log.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 25 | #include <fcntl.h> |
Nikita Ioffe | 29e15c8 | 2023-02-25 02:31:51 +0000 | [diff] [blame] | 26 | #include <fstab/fstab.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 27 | #include <fsverity_digests.pb.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 28 | #include <linux/vm_sockets.h> |
| 29 | #include <stdint.h> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 30 | #include <stdio.h> |
Nikita Ioffe | 3452ee2 | 2022-12-15 00:31:56 +0000 | [diff] [blame] | 31 | #include <sys/capability.h> |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 32 | #include <sys/system_properties.h> |
Nikita Ioffe | aa9f603 | 2025-01-21 18:06:46 +0000 | [diff] [blame^] | 33 | #ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__ |
| 34 | #include <unicode/uchar.h> |
| 35 | #endif |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 36 | #include <unistd.h> |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 37 | #include <vm_main.h> |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 38 | #include <vm_payload_restricted.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 39 | |
Nikita Ioffe | d5846dc | 2024-11-01 18:44:45 +0000 | [diff] [blame] | 40 | #include <cstdint> |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 41 | #include <string> |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 42 | #include <thread> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 43 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 44 | using android::base::borrowed_fd; |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 45 | using android::base::ErrnoError; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 46 | using android::base::Error; |
Nikita Ioffe | 3452ee2 | 2022-12-15 00:31:56 +0000 | [diff] [blame] | 47 | using android::base::make_scope_guard; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 48 | using android::base::Result; |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 49 | using android::base::unique_fd; |
Nikita Ioffe | 29e15c8 | 2023-02-25 02:31:51 +0000 | [diff] [blame] | 50 | using android::fs_mgr::Fstab; |
| 51 | using android::fs_mgr::FstabEntry; |
| 52 | using android::fs_mgr::GetEntryForMountPoint; |
| 53 | using android::fs_mgr::ReadFstabFromFile; |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 54 | |
| 55 | using aidl::com::android::microdroid::testservice::BnTestService; |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 56 | using aidl::com::android::microdroid::testservice::BnVmCallback; |
| 57 | using aidl::com::android::microdroid::testservice::IAppCallback; |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 58 | using ndk::ScopedAStatus; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 59 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 60 | extern void testlib_sub(); |
| 61 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 62 | namespace { |
| 63 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 64 | constexpr char TAG[] = "testbinary"; |
| 65 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 66 | template <typename T> |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 67 | Result<T> report_test(std::string name, Result<T> result) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 68 | auto property = "debug.microdroid.test." + name; |
| 69 | std::stringstream outcome; |
| 70 | if (result.ok()) { |
| 71 | outcome << "PASS"; |
| 72 | } else { |
| 73 | outcome << "FAIL: " << result.error(); |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 74 | // Log the error in case the property is truncated. |
| 75 | std::string message = name + ": " + outcome.str(); |
| 76 | __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str()); |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 77 | } |
| 78 | __system_property_set(property.c_str(), outcome.str().c_str()); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 79 | return result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 82 | Result<void> run_echo_reverse_server(borrowed_fd listening_fd) { |
| 83 | struct sockaddr_vm client_sa = {}; |
| 84 | socklen_t client_sa_len = sizeof(client_sa); |
| 85 | unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len, |
| 86 | SOCK_CLOEXEC)}; |
| 87 | if (!connect_fd.ok()) { |
| 88 | return ErrnoError() << "Failed to accept vsock connection"; |
| 89 | } |
| 90 | |
| 91 | unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)}; |
| 92 | if (!input_fd.ok()) { |
| 93 | return ErrnoError() << "Failed to dup"; |
| 94 | } |
| 95 | FILE* input = fdopen(input_fd.release(), "r"); |
| 96 | if (!input) { |
| 97 | return ErrnoError() << "Failed to fdopen"; |
| 98 | } |
| 99 | |
David Brazdil | bcce351 | 2023-02-23 15:32:55 +0000 | [diff] [blame] | 100 | // Run forever, reverse one line at a time. |
| 101 | while (true) { |
| 102 | char* line = nullptr; |
| 103 | size_t size = 0; |
| 104 | if (getline(&line, &size, input) < 0) { |
Jiyong Park | 2ff0788 | 2024-12-17 11:30:28 +0900 | [diff] [blame] | 105 | if (errno == 0) { |
| 106 | return {}; // the input was closed |
| 107 | } |
David Brazdil | bcce351 | 2023-02-23 15:32:55 +0000 | [diff] [blame] | 108 | return ErrnoError() << "Failed to read"; |
| 109 | } |
| 110 | |
| 111 | std::string_view original = line; |
| 112 | if (!original.empty() && original.back() == '\n') { |
| 113 | original = original.substr(0, original.size() - 1); |
| 114 | } |
| 115 | |
| 116 | std::string reversed(original.rbegin(), original.rend()); |
| 117 | reversed += "\n"; |
| 118 | |
| 119 | if (write(connect_fd, reversed.data(), reversed.size()) < 0) { |
| 120 | return ErrnoError() << "Failed to write"; |
| 121 | } |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 122 | } |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Result<void> start_echo_reverse_server() { |
| 126 | unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))}; |
| 127 | if (!server_fd.ok()) { |
| 128 | return ErrnoError() << "Failed to create vsock socket"; |
| 129 | } |
| 130 | struct sockaddr_vm server_sa = (struct sockaddr_vm){ |
| 131 | .svm_family = AF_VSOCK, |
Alan Stokes | 10c4767 | 2022-12-13 17:17:08 +0000 | [diff] [blame] | 132 | .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT), |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 133 | .svm_cid = VMADDR_CID_ANY, |
| 134 | }; |
| 135 | int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa))); |
| 136 | if (ret < 0) { |
| 137 | return ErrnoError() << "Failed to bind vsock socket"; |
| 138 | } |
| 139 | ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1)); |
| 140 | if (ret < 0) { |
| 141 | return ErrnoError() << "Failed to listen"; |
| 142 | } |
| 143 | |
| 144 | std::thread accept_thread{[listening_fd = std::move(server_fd)] { |
Jiyong Park | 2ff0788 | 2024-12-17 11:30:28 +0900 | [diff] [blame] | 145 | Result<void> result; |
| 146 | while ((result = run_echo_reverse_server(listening_fd)).ok()) { |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 147 | } |
Jiyong Park | 2ff0788 | 2024-12-17 11:30:28 +0900 | [diff] [blame] | 148 | __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str()); |
| 149 | // Make sure the VM exits so the test will fail solidly |
| 150 | exit(1); |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 151 | }}; |
| 152 | accept_thread.detach(); |
| 153 | |
| 154 | return {}; |
| 155 | } |
| 156 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 157 | Result<void> start_test_service() { |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 158 | class VmCallbackImpl : public BnVmCallback { |
| 159 | private: |
| 160 | std::shared_ptr<IAppCallback> mAppCallback; |
| 161 | |
| 162 | public: |
| 163 | explicit VmCallbackImpl(const std::shared_ptr<IAppCallback>& appCallback) |
| 164 | : mAppCallback(appCallback) {} |
| 165 | |
| 166 | ScopedAStatus echoMessage(const std::string& message) override { |
| 167 | std::thread callback_thread{[=, appCallback = mAppCallback] { |
| 168 | appCallback->onEchoRequestReceived("Received: " + message); |
| 169 | }}; |
| 170 | callback_thread.detach(); |
| 171 | return ScopedAStatus::ok(); |
| 172 | } |
| 173 | }; |
| 174 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 175 | class TestService : public BnTestService { |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 176 | public: |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 177 | ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 178 | *out = a + b; |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 179 | return ScopedAStatus::ok(); |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 180 | } |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 181 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 182 | ScopedAStatus readProperty(const std::string& prop, std::string* out) override { |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 183 | *out = android::base::GetProperty(prop, ""); |
| 184 | if (out->empty()) { |
| 185 | std::string msg = "cannot find property " + prop; |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 186 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 187 | msg.c_str()); |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 190 | return ScopedAStatus::ok(); |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 191 | } |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 192 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 193 | ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override { |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 194 | const uint8_t identifier[] = {1, 2, 3, 4}; |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 195 | out->resize(32); |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 196 | AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(), |
| 197 | out->size()); |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 198 | return ScopedAStatus::ok(); |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 199 | } |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 200 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 201 | ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override { |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 202 | size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0); |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 203 | out->resize(cdi_size); |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 204 | AVmPayload_getDiceAttestationCdi(out->data(), out->size()); |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 205 | return ScopedAStatus::ok(); |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 206 | } |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 207 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 208 | ScopedAStatus getBcc(std::vector<uint8_t>* out) override { |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 209 | size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0); |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 210 | out->resize(bcc_size); |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 211 | AVmPayload_getDiceAttestationChain(out->data(), out->size()); |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 212 | return ScopedAStatus::ok(); |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 213 | } |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 214 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 215 | ScopedAStatus getApkContentsPath(std::string* out) override { |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 216 | const char* path_c = AVmPayload_getApkContentsPath(); |
| 217 | if (path_c == nullptr) { |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 218 | return ScopedAStatus:: |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 219 | fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path"); |
| 220 | } |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 221 | *out = path_c; |
| 222 | return ScopedAStatus::ok(); |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 223 | } |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 224 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 225 | ScopedAStatus getEncryptedStoragePath(std::string* out) override { |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 226 | const char* path_c = AVmPayload_getEncryptedStoragePath(); |
| 227 | if (path_c == nullptr) { |
| 228 | out->clear(); |
| 229 | } else { |
| 230 | *out = path_c; |
| 231 | } |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 232 | return ScopedAStatus::ok(); |
| 233 | } |
| 234 | |
Nikita Ioffe | 3452ee2 | 2022-12-15 00:31:56 +0000 | [diff] [blame] | 235 | ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override { |
| 236 | if (out == nullptr) { |
| 237 | return ScopedAStatus::ok(); |
| 238 | } |
| 239 | cap_t cap = cap_get_proc(); |
| 240 | auto guard = make_scope_guard([&cap]() { cap_free(cap); }); |
| 241 | for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) { |
| 242 | cap_flag_value_t value; |
| 243 | if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) { |
| 244 | return ScopedAStatus:: |
| 245 | fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed"); |
| 246 | } |
| 247 | if (value == CAP_SET) { |
| 248 | // Ideally we would just send back the cap_ids, but I wasn't able to find java |
| 249 | // APIs for linux capabilities, hence we transform to the human readable name |
| 250 | // here. |
| 251 | char* name = cap_to_name(cap_id); |
| 252 | out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")"); |
| 253 | } |
| 254 | } |
| 255 | return ScopedAStatus::ok(); |
| 256 | } |
| 257 | |
Alan Stokes | 1294f94 | 2023-08-21 14:34:12 +0100 | [diff] [blame] | 258 | ScopedAStatus getUid(int* out) override { |
| 259 | *out = getuid(); |
| 260 | return ScopedAStatus::ok(); |
| 261 | } |
| 262 | |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 263 | ScopedAStatus runEchoReverseServer() override { |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 264 | auto result = start_echo_reverse_server(); |
| 265 | if (result.ok()) { |
| 266 | return ScopedAStatus::ok(); |
| 267 | } else { |
| 268 | std::string message = result.error().message(); |
| 269 | return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str()); |
| 270 | } |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 271 | } |
Shikha Panwar | dc11b7e | 2022-12-15 12:24:11 +0000 | [diff] [blame] | 272 | |
| 273 | ScopedAStatus writeToFile(const std::string& content, const std::string& path) override { |
| 274 | if (!android::base::WriteStringToFile(content, path)) { |
| 275 | std::string msg = "Failed to write " + content + " to file " + path + |
| 276 | ". Errono: " + std::to_string(errno); |
| 277 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 278 | msg.c_str()); |
| 279 | } |
Shikha Panwar | dc11b7e | 2022-12-15 12:24:11 +0000 | [diff] [blame] | 280 | return ScopedAStatus::ok(); |
| 281 | } |
| 282 | |
| 283 | ScopedAStatus readFromFile(const std::string& path, std::string* out) override { |
| 284 | if (!android::base::ReadFileToString(path, out)) { |
| 285 | std::string msg = |
| 286 | "Failed to read " + path + " to string. Errono: " + std::to_string(errno); |
| 287 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 288 | msg.c_str()); |
| 289 | } |
| 290 | return ScopedAStatus::ok(); |
| 291 | } |
Shikha Panwar | def7ef9 | 2023-01-06 08:35:48 +0000 | [diff] [blame] | 292 | |
Nikita Ioffe | a7cb367 | 2023-02-24 23:10:34 +0000 | [diff] [blame] | 293 | ScopedAStatus getFilePermissions(const std::string& path, int32_t* out) override { |
| 294 | struct stat sb; |
| 295 | if (stat(path.c_str(), &sb) != -1) { |
| 296 | *out = sb.st_mode; |
| 297 | } else { |
| 298 | std::string msg = "stat " + path + " failed : " + std::strerror(errno); |
| 299 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 300 | msg.c_str()); |
| 301 | } |
| 302 | return ScopedAStatus::ok(); |
| 303 | } |
| 304 | |
Nikita Ioffe | 29e15c8 | 2023-02-25 02:31:51 +0000 | [diff] [blame] | 305 | ScopedAStatus getMountFlags(const std::string& mount_point, int32_t* out) override { |
| 306 | Fstab fstab; |
| 307 | if (!ReadFstabFromFile("/proc/mounts", &fstab)) { |
| 308 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 309 | "Failed to read /proc/mounts"); |
| 310 | } |
| 311 | FstabEntry* entry = GetEntryForMountPoint(&fstab, mount_point); |
| 312 | if (entry == nullptr) { |
| 313 | std::string msg = mount_point + " not found in /proc/mounts"; |
| 314 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 315 | msg.c_str()); |
| 316 | } |
| 317 | *out = entry->flags; |
| 318 | return ScopedAStatus::ok(); |
| 319 | } |
| 320 | |
Nikita Ioffe | d5846dc | 2024-11-01 18:44:45 +0000 | [diff] [blame] | 321 | ScopedAStatus getPageSize(int32_t* out) override { |
| 322 | *out = getpagesize(); |
| 323 | return ScopedAStatus::ok(); |
| 324 | } |
| 325 | |
Alan Stokes | 63fa37b | 2023-02-22 14:56:57 +0000 | [diff] [blame] | 326 | ScopedAStatus requestCallback(const std::shared_ptr<IAppCallback>& appCallback) { |
| 327 | auto vmCallback = ndk::SharedRefBase::make<VmCallbackImpl>(appCallback); |
| 328 | std::thread callback_thread{[=] { appCallback->setVmCallback(vmCallback); }}; |
| 329 | callback_thread.detach(); |
| 330 | return ScopedAStatus::ok(); |
| 331 | } |
| 332 | |
Jiyong Park | 92e3472 | 2023-06-27 00:43:39 +0900 | [diff] [blame] | 333 | ScopedAStatus readLineFromConsole(std::string* out) { |
| 334 | FILE* f = fopen("/dev/console", "r"); |
| 335 | if (f == nullptr) { |
| 336 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 337 | "failed to open /dev/console"); |
| 338 | } |
| 339 | char* line = nullptr; |
| 340 | size_t len = 0; |
| 341 | ssize_t nread = getline(&line, &len, f); |
| 342 | |
| 343 | if (nread == -1) { |
| 344 | free(line); |
| 345 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 346 | "failed to read /dev/console"); |
| 347 | } |
| 348 | out->append(line, nread); |
| 349 | free(line); |
| 350 | return ScopedAStatus::ok(); |
| 351 | } |
| 352 | |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 353 | ScopedAStatus insecurelyReadPayloadRpData(std::array<uint8_t, 32>* out) override { |
Nikita Ioffe | 3e3fdf0 | 2025-01-15 14:20:27 +0000 | [diff] [blame] | 354 | if (__builtin_available(android 36, *)) { |
| 355 | int32_t ret = AVmPayload_readRollbackProtectedSecret(out->data(), 32); |
| 356 | if (ret != 32) { |
| 357 | return ScopedAStatus::fromServiceSpecificError(ret); |
| 358 | } |
| 359 | return ScopedAStatus::ok(); |
| 360 | } else { |
| 361 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 362 | "not available before SDK 36"); |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 363 | } |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | ScopedAStatus insecurelyWritePayloadRpData( |
| 367 | const std::array<uint8_t, 32>& inputData) override { |
Nikita Ioffe | 3e3fdf0 | 2025-01-15 14:20:27 +0000 | [diff] [blame] | 368 | if (__builtin_available(android 36, *)) { |
| 369 | int32_t ret = AVmPayload_writeRollbackProtectedSecret(inputData.data(), 32); |
| 370 | if (ret != 32) { |
| 371 | return ScopedAStatus::fromServiceSpecificError(ret); |
| 372 | } |
| 373 | return ScopedAStatus::ok(); |
| 374 | } else { |
| 375 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 376 | "not available before SDK 36"); |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 377 | } |
Shikha Panwar | 0c3a2fa | 2024-12-06 18:38:06 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Shikha Panwar | 5b7b494 | 2024-12-18 15:32:49 +0000 | [diff] [blame] | 380 | ScopedAStatus isNewInstance(bool* is_new_instance_out) override { |
Nikita Ioffe | 3e3fdf0 | 2025-01-15 14:20:27 +0000 | [diff] [blame] | 381 | if (__builtin_available(android 36, *)) { |
| 382 | *is_new_instance_out = AVmPayload_isNewInstance(); |
| 383 | return ScopedAStatus::ok(); |
| 384 | } else { |
| 385 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 386 | "not available before SDK 36"); |
| 387 | } |
Shikha Panwar | 5b7b494 | 2024-12-18 15:32:49 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Nikita Ioffe | 901083f | 2025-01-20 01:54:28 +0000 | [diff] [blame] | 390 | ScopedAStatus checkLibIcuIsAccessible() override { |
Nikita Ioffe | aa9f603 | 2025-01-21 18:06:46 +0000 | [diff] [blame^] | 391 | #ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__ |
Nikita Ioffe | 901083f | 2025-01-20 01:54:28 +0000 | [diff] [blame] | 392 | static constexpr const char* kLibIcuPath = "/apex/com.android.i18n/lib64/libicu.so"; |
| 393 | if (access(kLibIcuPath, R_OK) == 0) { |
Nikita Ioffe | aa9f603 | 2025-01-21 18:06:46 +0000 | [diff] [blame^] | 394 | if (!u_hasBinaryProperty(U'❤' /* Emoji heart U+2764 */, UCHAR_EMOJI)) { |
| 395 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 396 | "libicu broken!"); |
| 397 | } |
Nikita Ioffe | 901083f | 2025-01-20 01:54:28 +0000 | [diff] [blame] | 398 | return ScopedAStatus::ok(); |
| 399 | } else { |
| 400 | std::string msg = "failed to access " + std::string(kLibIcuPath) + "(" + |
| 401 | std::to_string(errno) + ")"; |
| 402 | return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 403 | msg.c_str()); |
| 404 | } |
Nikita Ioffe | aa9f603 | 2025-01-21 18:06:46 +0000 | [diff] [blame^] | 405 | #else |
| 406 | return ScopedAStatus:: |
| 407 | fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 408 | "should be only used together with " |
| 409 | "MicrodroidTestNativeLibWithLibIcu.so payload"); |
| 410 | #endif |
Nikita Ioffe | 901083f | 2025-01-20 01:54:28 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Shikha Panwar | def7ef9 | 2023-01-06 08:35:48 +0000 | [diff] [blame] | 413 | ScopedAStatus quit() override { exit(0); } |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 414 | }; |
| 415 | auto testService = ndk::SharedRefBase::make<TestService>(); |
| 416 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 417 | auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); }; |
Devin Moore | caf7b95 | 2023-04-24 20:14:35 +0000 | [diff] [blame] | 418 | AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->PORT, callback, |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 419 | nullptr); |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 420 | |
| 421 | return {}; |
| 422 | } |
| 423 | |
Alan Stokes | 4b31a48 | 2024-02-01 17:56:06 +0000 | [diff] [blame] | 424 | Result<void> verify_build_manifest() { |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 425 | const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb"; |
| 426 | |
| 427 | std::string str; |
| 428 | if (!android::base::ReadFileToString(path, &str)) { |
| 429 | return ErrnoError() << "failed to read build_manifest.pb"; |
| 430 | } |
| 431 | |
| 432 | if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) { |
| 433 | return Error() << "invalid build_manifest.pb"; |
| 434 | } |
| 435 | |
| 436 | return {}; |
| 437 | } |
| 438 | |
Alan Stokes | 4b31a48 | 2024-02-01 17:56:06 +0000 | [diff] [blame] | 439 | Result<void> verify_vm_share() { |
| 440 | const char* path = "/mnt/extra-apk/0/assets/vmshareapp.txt"; |
| 441 | |
| 442 | std::string str; |
| 443 | if (!android::base::ReadFileToString(path, &str)) { |
| 444 | return ErrnoError() << "failed to read vmshareapp.txt"; |
| 445 | } |
| 446 | |
| 447 | return {}; |
| 448 | } |
| 449 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 450 | } // Anonymous namespace |
| 451 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 452 | extern "C" int AVmPayload_main() { |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 453 | __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid"); |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 454 | |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 455 | // Make sure we can call into other shared libraries. |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 456 | testlib_sub(); |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 457 | |
Alan Stokes | 4b31a48 | 2024-02-01 17:56:06 +0000 | [diff] [blame] | 458 | // Report various things that aren't always fatal - these are checked in MicrodroidTests as |
| 459 | // appropriate. |
| 460 | report_test("extra_apk_build_manifest", verify_build_manifest()); |
| 461 | report_test("extra_apk_vm_share", verify_vm_share()); |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 462 | |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 463 | __system_property_set("debug.microdroid.app.run", "true"); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 464 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 465 | if (auto res = start_test_service(); res.ok()) { |
| 466 | return 0; |
| 467 | } else { |
Alan Stokes | d1a30dd | 2022-11-30 09:39:54 +0000 | [diff] [blame] | 468 | __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str()); |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 469 | return 1; |
| 470 | } |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 471 | } |