blob: c3f9988efd664fd3bae56842430af48e14e44684 [file] [log] [blame]
Jiyong Park40699612021-05-24 16:55:06 +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 */
16
Inseob Kim7116f792022-12-09 14:51:23 +090017#include <android-base/logging.h>
18#include <android-base/result.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010019#include <android/dlext.h>
Jiyong Park40699612021-05-24 16:55:06 +090020#include <dlfcn.h>
21
22#include <cstdlib>
23#include <iostream>
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090024#include <string>
25
Alan Stokes52d3c722022-10-04 17:27:13 +010026#include "vm_main.h"
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090027
Inseob Kim7116f792022-12-09 14:51:23 +090028using android::base::Error;
29using android::base::Result;
30
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090031extern "C" {
32enum {
33 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
34 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
35 ANDROID_NAMESPACE_TYPE_SHARED = 2,
36};
37
38extern 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 Kimc3ae5fd2022-11-30 18:26:56 +090042
43extern bool android_link_namespaces(struct android_namespace_t* from,
44 struct android_namespace_t* to,
45 const char* shared_libs_sonames);
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090046} // extern "C"
47
Inseob Kim7116f792022-12-09 14:51:23 +090048static Result<void*> load(const std::string& libname);
Jiyong Park40699612021-05-24 16:55:06 +090049
Alan Stokes52d3c722022-10-04 17:27:13 +010050constexpr char entrypoint_name[] = "AVmPayload_main";
51
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090052static 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 Park40699612021-05-24 16:55:06 +090057int main(int argc, char* argv[]) {
Alan Stokes52d3c722022-10-04 17:27:13 +010058 if (argc != 2) {
Jiyong Park40699612021-05-24 16:55:06 +090059 std::cout << "Usage:\n";
Alan Stokes52d3c722022-10-04 17:27:13 +010060 std::cout << " " << argv[0] << " LIBNAME\n";
Jiyong Park40699612021-05-24 16:55:06 +090061 return EXIT_FAILURE;
62 }
63
Inseob Kim7116f792022-12-09 14:51:23 +090064 android::base::InitLogging(argv, &android::base::KernelLogger);
65
Jiyong Park40699612021-05-24 16:55:06 +090066 const char* libname = argv[1];
Inseob Kim7116f792022-12-09 14:51:23 +090067 auto handle = load(libname);
68 if (!handle.ok()) {
69 LOG(ERROR) << "Failed to load " << libname << ": " << handle.error().message();
Jiyong Park40699612021-05-24 16:55:06 +090070 return EXIT_FAILURE;
71 }
72
Inseob Kim7116f792022-12-09 14:51:23 +090073 AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(*handle, entrypoint_name));
Jiyong Park40699612021-05-24 16:55:06 +090074 if (entry == nullptr) {
Inseob Kim7116f792022-12-09 14:51:23 +090075 LOG(ERROR) << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror();
Jiyong Park40699612021-05-24 16:55:06 +090076 return EXIT_FAILURE;
77 }
78
Alan Stokes52d3c722022-10-04 17:27:13 +010079 return entry();
Jiyong Park40699612021-05-24 16:55:06 +090080}
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090081
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 Kim7116f792022-12-09 14:51:23 +090085Result<void*> load(const std::string& libname) {
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090086 // Parent as nullptr means the default namespace
87 android_namespace_t* parent = nullptr;
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090088 // The search paths of the new namespace are isolated to restrict system private libraries.
89 const uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED;
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090090 // 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 Kim7116f792022-12-09 14:51:23 +090099 return Error() << "Failed to create linker namespace: " << dlerror();
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900100 }
101
Inseob Kimc3ae5fd2022-11-30 18:26:56 +0900102 std::string libs;
103 for (const char* lib : kAllowedLibs) {
104 if (!libs.empty()) libs += ':';
105 libs += lib;
106 }
Inseob Kim7116f792022-12-09 14:51:23 +0900107 if (!android_link_namespaces(new_ns, nullptr, libs.c_str())) {
108 return Error() << "Failed to link namespace: " << dlerror();
109 }
Inseob Kimc3ae5fd2022-11-30 18:26:56 +0900110
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900111 const android_dlextinfo info = {
112 .flags = ANDROID_DLEXT_USE_NAMESPACE,
113 .library_namespace = new_ns,
114 };
Inseob Kim7116f792022-12-09 14:51:23 +0900115 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 Parkfe5b28e2021-06-24 00:19:02 +0900120}