1 /*
2 * Copyright (C) 2019 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 #if defined(ART_TARGET_ANDROID)
18
19 #define LOG_TAG "nativeloader"
20
21 #include "native_loader_namespace.h"
22
23 #include <dlfcn.h>
24
25 #include <functional>
26
27 #include <android-base/strings.h>
28 #include <log/log.h>
29 #include <nativebridge/native_bridge.h>
30
31 #include "nativeloader/dlext_namespaces.h"
32
33 using android::base::Error;
34
35 namespace android {
36
37 namespace {
38
39 constexpr const char* kDefaultNamespaceName = "default";
40 constexpr const char* kSystemNamespaceName = "system";
41
GetLinkerError(bool is_bridged)42 std::string GetLinkerError(bool is_bridged) {
43 const char* msg = is_bridged ? NativeBridgeGetError() : dlerror();
44 if (msg == nullptr) {
45 return "no error";
46 }
47 return std::string(msg);
48 }
49
50 } // namespace
51
GetExportedNamespace(const std::string & name,bool is_bridged)52 Result<NativeLoaderNamespace> NativeLoaderNamespace::GetExportedNamespace(const std::string& name,
53 bool is_bridged) {
54 if (!is_bridged) {
55 android_namespace_t* raw = android_get_exported_namespace(name.c_str());
56 if (raw != nullptr) {
57 return NativeLoaderNamespace(name, raw);
58 }
59 } else {
60 native_bridge_namespace_t* raw = NativeBridgeGetExportedNamespace(name.c_str());
61 if (raw != nullptr) {
62 return NativeLoaderNamespace(name, raw);
63 }
64 }
65 return Errorf("namespace {} does not exist or exported", name);
66 }
67
68 // The system namespace is called "default" for binaries in /system and
69 // "system" for those in the Runtime APEX. Try "system" first since
70 // "default" always exists.
GetSystemNamespace(bool is_bridged)71 Result<NativeLoaderNamespace> NativeLoaderNamespace::GetSystemNamespace(bool is_bridged) {
72 if (Result<NativeLoaderNamespace> ns = GetExportedNamespace(kSystemNamespaceName, is_bridged);
73 ns.ok()) {
74 return ns;
75 }
76 if (Result<NativeLoaderNamespace> ns = GetExportedNamespace(kDefaultNamespaceName, is_bridged);
77 ns.ok()) {
78 return ns;
79 }
80
81 // If nothing is found, return NativeLoaderNamespace constructed from nullptr.
82 // nullptr also means default namespace to the linker.
83 if (!is_bridged) {
84 return NativeLoaderNamespace(kDefaultNamespaceName, static_cast<android_namespace_t*>(nullptr));
85 } else {
86 return NativeLoaderNamespace(kDefaultNamespaceName,
87 static_cast<native_bridge_namespace_t*>(nullptr));
88 }
89 }
90
Create(const std::string & name,const std::string & search_paths,const std::string & permitted_paths,const NativeLoaderNamespace * parent,bool is_shared,bool is_exempt_list_enabled,bool also_used_as_anonymous)91 Result<NativeLoaderNamespace> NativeLoaderNamespace::Create(
92 const std::string& name, const std::string& search_paths, const std::string& permitted_paths,
93 const NativeLoaderNamespace* parent, bool is_shared, bool is_exempt_list_enabled,
94 bool also_used_as_anonymous) {
95 bool is_bridged = false;
96 if (parent != nullptr) {
97 is_bridged = parent->IsBridged();
98 } else if (!search_paths.empty()) {
99 is_bridged = NativeBridgeIsPathSupported(search_paths.c_str());
100 }
101
102 // Fall back to the system namespace if no parent is set.
103 Result<NativeLoaderNamespace> system_ns = GetSystemNamespace(is_bridged);
104 if (!system_ns.ok()) {
105 return system_ns.error();
106 }
107 const NativeLoaderNamespace& effective_parent = parent != nullptr ? *parent : *system_ns;
108
109 // All namespaces for apps are isolated
110 uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED;
111
112 // The namespace is also used as the anonymous namespace
113 // which is used when the linker fails to determine the caller address
114 if (also_used_as_anonymous) {
115 type |= ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
116 }
117
118 // Bundled apps have access to all system libraries that are currently loaded
119 // in the default namespace
120 if (is_shared) {
121 type |= ANDROID_NAMESPACE_TYPE_SHARED;
122 }
123 if (is_exempt_list_enabled) {
124 type |= ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED;
125 }
126
127 if (!is_bridged) {
128 android_namespace_t* raw =
129 android_create_namespace(name.c_str(), nullptr, search_paths.c_str(), type,
130 permitted_paths.c_str(), effective_parent.ToRawAndroidNamespace());
131 if (raw != nullptr) {
132 return NativeLoaderNamespace(name, raw);
133 }
134 } else {
135 native_bridge_namespace_t* raw = NativeBridgeCreateNamespace(
136 name.c_str(), nullptr, search_paths.c_str(), type, permitted_paths.c_str(),
137 effective_parent.ToRawNativeBridgeNamespace());
138 if (raw != nullptr) {
139 return NativeLoaderNamespace(name, raw);
140 }
141 }
142 return Errorf("failed to create {} namespace name:{}, search_paths:{}, permitted_paths:{}",
143 is_bridged ? "bridged" : "native", name, search_paths, permitted_paths);
144 }
145
Link(const NativeLoaderNamespace * target,const std::string & shared_libs) const146 Result<void> NativeLoaderNamespace::Link(const NativeLoaderNamespace* target,
147 const std::string& shared_libs) const {
148 LOG_ALWAYS_FATAL_IF(shared_libs.empty(), "empty share lib when linking %s to %s",
149 this->name().c_str(), target == nullptr ? "default" : target->name().c_str());
150 if (!IsBridged()) {
151 if (android_link_namespaces(this->ToRawAndroidNamespace(),
152 target == nullptr ? nullptr : target->ToRawAndroidNamespace(),
153 shared_libs.c_str())) {
154 return {};
155 }
156 } else {
157 if (NativeBridgeLinkNamespaces(this->ToRawNativeBridgeNamespace(),
158 target == nullptr ? nullptr : target->ToRawNativeBridgeNamespace(),
159 shared_libs.c_str())) {
160 return {};
161 }
162 }
163 return Error() << GetLinkerError(IsBridged());
164 }
165
Load(const char * lib_name) const166 Result<void*> NativeLoaderNamespace::Load(const char* lib_name) const {
167 if (!IsBridged()) {
168 android_dlextinfo extinfo;
169 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
170 extinfo.library_namespace = this->ToRawAndroidNamespace();
171 void* handle = android_dlopen_ext(lib_name, RTLD_NOW, &extinfo);
172 if (handle != nullptr) {
173 return handle;
174 }
175 } else {
176 void* handle =
177 NativeBridgeLoadLibraryExt(lib_name, RTLD_NOW, this->ToRawNativeBridgeNamespace());
178 if (handle != nullptr) {
179 return handle;
180 }
181 }
182 return Error() << GetLinkerError(IsBridged());
183 }
184
185 } // namespace android
186
187 #endif // defined(ART_TARGET_ANDROID)
188