Jiyong Park | 4069961 | 2021-05-24 16:55:06 +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 | */ |
| 16 | |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 17 | #include <android-base/logging.h> |
| 18 | #include <android-base/result.h> |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 19 | #include <android/dlext.h> |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 20 | #include <dlfcn.h> |
| 21 | |
| 22 | #include <cstdlib> |
| 23 | #include <iostream> |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 26 | #include "vm_main.h" |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 27 | |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 28 | using android::base::Error; |
| 29 | using android::base::Result; |
| 30 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 31 | extern "C" { |
| 32 | enum { |
| 33 | ANDROID_NAMESPACE_TYPE_REGULAR = 0, |
| 34 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 35 | ANDROID_NAMESPACE_TYPE_SHARED = 2, |
| 36 | }; |
| 37 | |
| 38 | extern struct android_namespace_t* android_create_namespace( |
| 39 | const char* name, const char* ld_library_path, const char* default_library_path, |
| 40 | uint64_t type, const char* permitted_when_isolated_path, |
| 41 | struct android_namespace_t* parent); |
Inseob Kim | c3ae5fd | 2022-11-30 18:26:56 +0900 | [diff] [blame] | 42 | |
| 43 | extern bool android_link_namespaces(struct android_namespace_t* from, |
| 44 | struct android_namespace_t* to, |
| 45 | const char* shared_libs_sonames); |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 46 | } // extern "C" |
| 47 | |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 48 | static Result<void*> load(const std::string& libname); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 49 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 50 | constexpr char entrypoint_name[] = "AVmPayload_main"; |
| 51 | |
Inseob Kim | c3ae5fd | 2022-11-30 18:26:56 +0900 | [diff] [blame] | 52 | static constexpr const char* kAllowedLibs[] = { |
| 53 | "libc.so", "libm.so", "libdl.so", "libdl_android.so", |
| 54 | "liblog.so", "libvm_payload.so", "libbinder_ndk.so", "libbinder_rpc_unstable.so", |
| 55 | }; |
| 56 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 57 | int main(int argc, char* argv[]) { |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 58 | if (argc != 2) { |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 59 | std::cout << "Usage:\n"; |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 60 | std::cout << " " << argv[0] << " LIBNAME\n"; |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 61 | return EXIT_FAILURE; |
| 62 | } |
| 63 | |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 64 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 65 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 66 | const char* libname = argv[1]; |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 67 | auto handle = load(libname); |
| 68 | if (!handle.ok()) { |
| 69 | LOG(ERROR) << "Failed to load " << libname << ": " << handle.error().message(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 70 | return EXIT_FAILURE; |
| 71 | } |
| 72 | |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 73 | AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(*handle, entrypoint_name)); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 74 | if (entry == nullptr) { |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 75 | LOG(ERROR) << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 76 | return EXIT_FAILURE; |
| 77 | } |
| 78 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 79 | return entry(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 80 | } |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 81 | |
| 82 | // Create a new linker namespace whose search path is set to the directory of the library. Then |
| 83 | // load it from there. Returns the handle to the loaded library if successful. Returns nullptr |
| 84 | // if failed. |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 85 | Result<void*> load(const std::string& libname) { |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 86 | // Parent as nullptr means the default namespace |
| 87 | android_namespace_t* parent = nullptr; |
Inseob Kim | c3ae5fd | 2022-11-30 18:26:56 +0900 | [diff] [blame] | 88 | // The search paths of the new namespace are isolated to restrict system private libraries. |
| 89 | const uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 90 | // The directory of the library is appended to the search paths |
| 91 | const std::string libdir = libname.substr(0, libname.find_last_of("/")); |
| 92 | const char* ld_library_path = libdir.c_str(); |
| 93 | const char* default_library_path = libdir.c_str(); |
| 94 | |
| 95 | android_namespace_t* new_ns = nullptr; |
| 96 | new_ns = android_create_namespace("microdroid_app", ld_library_path, default_library_path, type, |
| 97 | /* permitted_when_isolated_path */ nullptr, parent); |
| 98 | if (new_ns == nullptr) { |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 99 | return Error() << "Failed to create linker namespace: " << dlerror(); |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Inseob Kim | c3ae5fd | 2022-11-30 18:26:56 +0900 | [diff] [blame] | 102 | std::string libs; |
| 103 | for (const char* lib : kAllowedLibs) { |
| 104 | if (!libs.empty()) libs += ':'; |
| 105 | libs += lib; |
| 106 | } |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 107 | if (!android_link_namespaces(new_ns, nullptr, libs.c_str())) { |
| 108 | return Error() << "Failed to link namespace: " << dlerror(); |
| 109 | } |
Inseob Kim | c3ae5fd | 2022-11-30 18:26:56 +0900 | [diff] [blame] | 110 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 111 | const android_dlextinfo info = { |
| 112 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 113 | .library_namespace = new_ns, |
| 114 | }; |
Inseob Kim | 7116f79 | 2022-12-09 14:51:23 +0900 | [diff] [blame^] | 115 | if (auto ret = android_dlopen_ext(libname.c_str(), RTLD_NOW, &info); ret) { |
| 116 | return ret; |
| 117 | } else { |
| 118 | return Error() << "Failed to dlopen: " << dlerror(); |
| 119 | } |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 120 | } |