• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <android-base/logging.h>
18 #include <android-base/result.h>
19 #include <android/dlext.h>
20 #include <dlfcn.h>
21 
22 #include <cstdlib>
23 #include <iostream>
24 #include <string>
25 
26 #include "vm_main.h"
27 
28 using android::base::Error;
29 using android::base::Result;
30 
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);
42 
43 extern bool android_link_namespaces(struct android_namespace_t* from,
44                                     struct android_namespace_t* to,
45                                     const char* shared_libs_sonames);
46 } // extern "C"
47 
48 static Result<void*> load(const std::string& libname);
49 
50 constexpr char entrypoint_name[] = "AVmPayload_main";
51 
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 
main(int argc,char * argv[])57 int main(int argc, char* argv[]) {
58     if (argc != 2) {
59         std::cout << "Usage:\n";
60         std::cout << "    " << argv[0] << " LIBNAME\n";
61         return EXIT_FAILURE;
62     }
63 
64     android::base::InitLogging(argv, &android::base::KernelLogger);
65 
66     const char* libname = argv[1];
67     auto handle = load(libname);
68     if (!handle.ok()) {
69         LOG(ERROR) << "Failed to load " << libname << ": " << handle.error().message();
70         return EXIT_FAILURE;
71     }
72 
73     AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(*handle, entrypoint_name));
74     if (entry == nullptr) {
75         LOG(ERROR) << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror();
76         return EXIT_FAILURE;
77     }
78 
79     return entry();
80 }
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.
load(const std::string & libname)85 Result<void*> load(const std::string& libname) {
86     // Parent as nullptr means the default namespace
87     android_namespace_t* parent = nullptr;
88     // The search paths of the new namespace are isolated to restrict system private libraries.
89     const uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED;
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) {
99         return Error() << "Failed to create linker namespace: " << dlerror();
100     }
101 
102     std::string libs;
103     for (const char* lib : kAllowedLibs) {
104         if (!libs.empty()) libs += ':';
105         libs += lib;
106     }
107     if (!android_link_namespaces(new_ns, nullptr, libs.c_str())) {
108         return Error() << "Failed to link namespace: " << dlerror();
109     }
110 
111     const android_dlextinfo info = {
112             .flags = ANDROID_DLEXT_USE_NAMESPACE,
113             .library_namespace = new_ns,
114     };
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     }
120 }
121