blob: ae55be9f45eb068f6ec41e9ae7e6e9c3be17d183 [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
Alan Stokes52d3c722022-10-04 17:27:13 +010017#include <android/dlext.h>
Jiyong Park40699612021-05-24 16:55:06 +090018#include <dlfcn.h>
19
20#include <cstdlib>
21#include <iostream>
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090022#include <string>
23
Alan Stokes52d3c722022-10-04 17:27:13 +010024#include "vm_main.h"
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090025
26extern "C" {
27enum {
28 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
29 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
30 ANDROID_NAMESPACE_TYPE_SHARED = 2,
31};
32
33extern struct android_namespace_t* android_create_namespace(
34 const char* name, const char* ld_library_path, const char* default_library_path,
35 uint64_t type, const char* permitted_when_isolated_path,
36 struct android_namespace_t* parent);
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090037
38extern bool android_link_namespaces(struct android_namespace_t* from,
39 struct android_namespace_t* to,
40 const char* shared_libs_sonames);
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090041} // extern "C"
42
43static void* load(const std::string& libname);
Jiyong Park40699612021-05-24 16:55:06 +090044
Alan Stokes52d3c722022-10-04 17:27:13 +010045constexpr char entrypoint_name[] = "AVmPayload_main";
46
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090047static constexpr const char* kAllowedLibs[] = {
48 "libc.so", "libm.so", "libdl.so", "libdl_android.so",
49 "liblog.so", "libvm_payload.so", "libbinder_ndk.so", "libbinder_rpc_unstable.so",
50};
51
Jiyong Park40699612021-05-24 16:55:06 +090052int main(int argc, char* argv[]) {
Alan Stokes52d3c722022-10-04 17:27:13 +010053 if (argc != 2) {
Jiyong Park40699612021-05-24 16:55:06 +090054 std::cout << "Usage:\n";
Alan Stokes52d3c722022-10-04 17:27:13 +010055 std::cout << " " << argv[0] << " LIBNAME\n";
Jiyong Park40699612021-05-24 16:55:06 +090056 return EXIT_FAILURE;
57 }
58
59 const char* libname = argv[1];
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090060 void* handle = load(libname);
Jiyong Park40699612021-05-24 16:55:06 +090061 if (handle == nullptr) {
62 std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n";
63 return EXIT_FAILURE;
64 }
65
Alan Stokes52d3c722022-10-04 17:27:13 +010066 AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(handle, entrypoint_name));
Jiyong Park40699612021-05-24 16:55:06 +090067 if (entry == nullptr) {
Alan Stokes52d3c722022-10-04 17:27:13 +010068 std::cerr << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror() << "\n";
Jiyong Park40699612021-05-24 16:55:06 +090069 return EXIT_FAILURE;
70 }
71
Alan Stokes52d3c722022-10-04 17:27:13 +010072 return entry();
Jiyong Park40699612021-05-24 16:55:06 +090073}
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090074
75// Create a new linker namespace whose search path is set to the directory of the library. Then
76// load it from there. Returns the handle to the loaded library if successful. Returns nullptr
77// if failed.
78void* load(const std::string& libname) {
79 // Parent as nullptr means the default namespace
80 android_namespace_t* parent = nullptr;
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090081 // The search paths of the new namespace are isolated to restrict system private libraries.
82 const uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED;
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090083 // The directory of the library is appended to the search paths
84 const std::string libdir = libname.substr(0, libname.find_last_of("/"));
85 const char* ld_library_path = libdir.c_str();
86 const char* default_library_path = libdir.c_str();
87
88 android_namespace_t* new_ns = nullptr;
89 new_ns = android_create_namespace("microdroid_app", ld_library_path, default_library_path, type,
90 /* permitted_when_isolated_path */ nullptr, parent);
91 if (new_ns == nullptr) {
92 std::cerr << "Failed to create linker namespace: " << dlerror() << "\n";
93 return nullptr;
94 }
95
Inseob Kimc3ae5fd2022-11-30 18:26:56 +090096 std::string libs;
97 for (const char* lib : kAllowedLibs) {
98 if (!libs.empty()) libs += ':';
99 libs += lib;
100 }
101 android_link_namespaces(new_ns, nullptr, libs.c_str());
102
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900103 const android_dlextinfo info = {
104 .flags = ANDROID_DLEXT_USE_NAMESPACE,
105 .library_namespace = new_ns,
106 };
107 return android_dlopen_ext(libname.c_str(), RTLD_NOW, &info);
108}