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 | */ |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 16 | #include <aidl/com/android/microdroid/testservice/BnTestService.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 17 | #include <android-base/file.h> |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 18 | #include <android-base/properties.h> |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 19 | #include <android-base/result.h> |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 20 | #include <android/binder_auto_utils.h> |
| 21 | #include <android/binder_manager.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 22 | #include <fcntl.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 23 | #include <fsverity_digests.pb.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 24 | #include <linux/vm_sockets.h> |
| 25 | #include <stdint.h> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 26 | #include <stdio.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 28 | #include <sys/system_properties.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 29 | #include <unistd.h> |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 30 | #include <vm_main.h> |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 31 | #include <vm_payload_restricted.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 32 | |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 33 | #include <string> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 34 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 35 | using android::base::ErrnoError; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 36 | using android::base::Error; |
| 37 | using android::base::Result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 38 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 39 | extern void testlib_sub(); |
| 40 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 43 | template <typename T> |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 44 | Result<T> report_test(std::string name, Result<T> result) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 45 | auto property = "debug.microdroid.test." + name; |
| 46 | std::stringstream outcome; |
| 47 | if (result.ok()) { |
| 48 | outcome << "PASS"; |
| 49 | } else { |
| 50 | outcome << "FAIL: " << result.error(); |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 51 | // Pollute stderr with the error in case the property is truncated. |
| 52 | std::cerr << "[" << name << "] test failed: " << result.error() << "\n"; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 53 | } |
| 54 | __system_property_set(property.c_str(), outcome.str().c_str()); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 55 | return result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 58 | Result<void> start_test_service() { |
| 59 | class TestService : public aidl::com::android::microdroid::testservice::BnTestService { |
| 60 | ndk::ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override { |
| 61 | *out = a + b; |
| 62 | return ndk::ScopedAStatus::ok(); |
| 63 | } |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 64 | |
| 65 | ndk::ScopedAStatus readProperty(const std::string& prop, std::string* out) override { |
| 66 | *out = android::base::GetProperty(prop, ""); |
| 67 | if (out->empty()) { |
| 68 | std::string msg = "cannot find property " + prop; |
| 69 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 70 | msg.c_str()); |
| 71 | } |
| 72 | |
| 73 | return ndk::ScopedAStatus::ok(); |
| 74 | } |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 75 | |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 76 | ndk::ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override { |
| 77 | const uint8_t identifier[] = {1, 2, 3, 4}; |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 78 | out->resize(32); |
| 79 | if (!AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(), |
| 80 | out->size())) { |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 81 | return ndk::ScopedAStatus:: |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 82 | fromServiceSpecificErrorWithMessage(0, "Failed to get VM instance secret"); |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 83 | } |
Andrew Scull | 2e6ab79 | 2022-01-30 16:04:08 +0000 | [diff] [blame] | 84 | return ndk::ScopedAStatus::ok(); |
| 85 | } |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 86 | |
| 87 | ndk::ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override { |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 88 | size_t cdi_size; |
| 89 | if (!AVmPayload_getDiceAttestationCdi(nullptr, 0, &cdi_size)) { |
| 90 | return ndk::ScopedAStatus:: |
| 91 | fromServiceSpecificErrorWithMessage(0, "Failed to measure attestation cdi"); |
| 92 | } |
| 93 | out->resize(cdi_size); |
| 94 | if (!AVmPayload_getDiceAttestationCdi(out->data(), out->size(), &cdi_size)) { |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 95 | return ndk::ScopedAStatus:: |
Andrew Scull | e4b0285 | 2022-10-06 18:53:56 +0000 | [diff] [blame] | 96 | fromServiceSpecificErrorWithMessage(0, "Failed to get attestation cdi"); |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 97 | } |
Andrew Scull | 1f6ca35 | 2022-02-20 22:51:12 +0000 | [diff] [blame] | 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 100 | |
| 101 | ndk::ScopedAStatus getBcc(std::vector<uint8_t>* out) override { |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 102 | size_t bcc_size; |
| 103 | if (!AVmPayload_getDiceAttestationChain(nullptr, 0, &bcc_size)) { |
| 104 | return ndk::ScopedAStatus:: |
| 105 | fromServiceSpecificErrorWithMessage(0, |
| 106 | "Failed to measure attestation chain"); |
| 107 | } |
| 108 | out->resize(bcc_size); |
| 109 | if (!AVmPayload_getDiceAttestationChain(out->data(), out->size(), &bcc_size)) { |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 110 | return ndk::ScopedAStatus:: |
Andrew Scull | e4b0285 | 2022-10-06 18:53:56 +0000 | [diff] [blame] | 111 | fromServiceSpecificErrorWithMessage(0, "Failed to get attestation chain"); |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 112 | } |
Andrew Scull | 6189208 | 2022-02-21 00:07:25 +0000 | [diff] [blame] | 113 | return ndk::ScopedAStatus::ok(); |
| 114 | } |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 115 | |
| 116 | ndk::ScopedAStatus getApkContentsPath(std::string* out) override { |
| 117 | const char* path_c = AVmPayload_getApkContentsPath(); |
| 118 | if (path_c == nullptr) { |
| 119 | return ndk::ScopedAStatus:: |
| 120 | fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path"); |
| 121 | } |
| 122 | std::string path(path_c); |
| 123 | *out = path; |
| 124 | return ndk::ScopedAStatus::ok(); |
| 125 | } |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame^] | 126 | |
| 127 | ndk::ScopedAStatus getEncryptedStoragePath(std::string* out) override { |
| 128 | const char* path_c = AVmPayload_getEncryptedStoragePath(); |
| 129 | if (path_c == nullptr) { |
| 130 | out->clear(); |
| 131 | } else { |
| 132 | *out = path_c; |
| 133 | } |
| 134 | return ndk::ScopedAStatus::ok(); |
| 135 | } |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 136 | }; |
| 137 | auto testService = ndk::SharedRefBase::make<TestService>(); |
| 138 | |
| 139 | auto callback = []([[maybe_unused]] void* param) { |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 140 | if (!AVmPayload_notifyPayloadReady()) { |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 141 | std::cerr << "failed to notify payload ready to virtualizationservice" << std::endl; |
Alan Stokes | b62de26 | 2022-05-12 17:11:53 +0100 | [diff] [blame] | 142 | abort(); |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 143 | } |
| 144 | }; |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 145 | if (!AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->SERVICE_PORT, |
| 146 | callback, nullptr)) { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 147 | return Error() << "RPC Server failed to run"; |
| 148 | } |
| 149 | |
| 150 | return {}; |
| 151 | } |
| 152 | |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 153 | Result<void> verify_apk() { |
| 154 | const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb"; |
| 155 | |
| 156 | std::string str; |
| 157 | if (!android::base::ReadFileToString(path, &str)) { |
| 158 | return ErrnoError() << "failed to read build_manifest.pb"; |
| 159 | } |
| 160 | |
| 161 | if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) { |
| 162 | return Error() << "invalid build_manifest.pb"; |
| 163 | } |
| 164 | |
| 165 | return {}; |
| 166 | } |
| 167 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 168 | } // Anonymous namespace |
| 169 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 170 | extern "C" int AVmPayload_main() { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 171 | // disable buffering to communicate seamlessly |
| 172 | setvbuf(stdin, nullptr, _IONBF, 0); |
| 173 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 174 | setvbuf(stderr, nullptr, _IONBF, 0); |
| 175 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 176 | printf("Hello Microdroid"); |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 177 | testlib_sub(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 178 | printf("\n"); |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 179 | |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 180 | // Extra apks may be missing; this is not a fatal error |
| 181 | report_test("extra_apk", verify_apk()); |
| 182 | |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 183 | __system_property_set("debug.microdroid.app.run", "true"); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 184 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 185 | if (auto res = start_test_service(); res.ok()) { |
| 186 | return 0; |
| 187 | } else { |
Alan Stokes | b62de26 | 2022-05-12 17:11:53 +0100 | [diff] [blame] | 188 | std::cerr << "starting service failed: " << res.error() << "\n"; |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 189 | return 1; |
| 190 | } |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 191 | } |