blob: 8e9b6ea6da8ebe053a28f422fcb81ae5e7273aad [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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#include "library_namespaces.h"
17
18#include <dirent.h>
19#include <dlfcn.h>
20
21#include <regex>
22#include <string>
23#include <vector>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/macros.h>
28#include <android-base/properties.h>
29#include <android-base/strings.h>
Orion Hodson6dc0a432020-02-06 14:28:28 +000030#include <nativehelper/scoped_utf_chars.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010031
32#include "nativeloader/dlext_namespaces.h"
33#include "public_libraries.h"
34#include "utils.h"
35
Orion Hodson9b16e342019-10-09 13:29:16 +010036namespace android::nativeloader {
37
38namespace {
39// The device may be configured to have the vendor libraries loaded to a separate namespace.
40// For historical reasons this namespace was named sphal but effectively it is intended
41// to use to load vendor libraries to separate namespace with controlled interface between
42// vendor and system namespaces.
43constexpr const char* kVendorNamespaceName = "sphal";
44constexpr const char* kVndkNamespaceName = "vndk";
Jooyung Han98cf82f2020-02-08 03:53:54 +090045constexpr const char* kArtNamespaceName = "com.android.art";
46constexpr const char* kNeuralNetworksNamespaceName = "com.android.neuralnetworks";
47constexpr const char* kCronetNamespaceName = "com.android.cronet";
Orion Hodson9b16e342019-10-09 13:29:16 +010048
49// classloader-namespace is a linker namespace that is created for the loaded
50// app. To be specific, it is created for the app classloader. When
51// System.load() is called from a Java class that is loaded from the
52// classloader, the classloader-namespace namespace associated with that
53// classloader is selected for dlopen. The namespace is configured so that its
54// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090055// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010056// This way an app can only load its own JNI libraries along with the public libs.
57constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
58// Same thing for vendor APKs.
59constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010060// If the namespace is shared then add this suffix to form
61// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
62// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
63// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090064// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010065// access to the platform libraries for apps bundled in the system image,
66// including their later updates installed in /data.
67constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010068
69// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
70// System.load() with an absolute path which is outside of the classloader library search path.
71// This list includes all directories app is allowed to access this way.
72constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
73
74constexpr const char* kVendorLibPath = "/vendor/" LIB;
75constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
76
77const std::regex kVendorDexPathRegex("(^|:)/vendor/");
78const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
79
80// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010081using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010082 APK_ORIGIN_DEFAULT = 0,
83 APK_ORIGIN_VENDOR = 1,
84 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010085};
Orion Hodson9b16e342019-10-09 13:29:16 +010086
87jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
88 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
89 jmethodID get_parent =
90 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
91
92 return env->CallObjectMethod(class_loader, get_parent);
93}
94
95ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
96 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
97
98 if (dex_path != nullptr) {
99 ScopedUtfChars dex_path_utf_chars(env, dex_path);
100
101 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
102 apk_origin = APK_ORIGIN_VENDOR;
103 }
104
105 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
106 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
107 "Dex path contains both vendor and product partition : %s",
108 dex_path_utf_chars.c_str());
109
110 apk_origin = APK_ORIGIN_PRODUCT;
111 }
112 }
113 return apk_origin;
114}
115
116} // namespace
117
118void LibraryNamespaces::Initialize() {
119 // Once public namespace is initialized there is no
120 // point in running this code - it will have no effect
121 // on the current list of public libraries.
122 if (initialized_) {
123 return;
124 }
125
126 // android_init_namespaces() expects all the public libraries
127 // to be loaded so that they can be found by soname alone.
128 //
129 // TODO(dimitry): this is a bit misleading since we do not know
130 // if the vendor public library is going to be opened from /vendor/lib
131 // we might as well end up loading them from /system/lib or /product/lib
132 // For now we rely on CTS test to catch things like this but
133 // it should probably be addressed in the future.
134 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
135 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
136 "Error preloading public library %s: %s", soname.c_str(), dlerror());
137 }
138}
139
140Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
141 jobject class_loader, bool is_shared,
142 jstring dex_path,
143 jstring java_library_path,
144 jstring java_permitted_path) {
145 std::string library_path; // empty string by default.
146
147 if (java_library_path != nullptr) {
148 ScopedUtfChars library_path_utf_chars(env, java_library_path);
149 library_path = library_path_utf_chars.c_str();
150 }
151
152 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
153
154 // (http://b/27588281) This is a workaround for apps using custom
155 // classloaders and calling System.load() with an absolute path which
156 // is outside of the classloader library search path.
157 //
158 // This part effectively allows such a classloader to access anything
159 // under /data and /mnt/expand
160 std::string permitted_path = kWhitelistedDirectories;
161
162 if (java_permitted_path != nullptr) {
163 ScopedUtfChars path(env, java_permitted_path);
164 if (path.c_str() != nullptr && path.size() > 0) {
165 permitted_path = permitted_path + ":" + path.c_str();
166 }
167 }
168
169 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
170 "There is already a namespace associated with this classloader");
171
172 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100173 std::string namespace_name = kClassloaderNamespaceName;
Orion Hodson9b16e342019-10-09 13:29:16 +0100174 bool unbundled_vendor_or_product_app = false;
175 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900176 (apk_origin == APK_ORIGIN_PRODUCT &&
177 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100178 !is_shared) {
179 unbundled_vendor_or_product_app = true;
180 // For vendor / product apks, give access to the vendor / product lib even though
181 // they are treated as unbundled; the libs and apks are still bundled
182 // together in the vendor / product partition.
183 const char* origin_partition;
184 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900185 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100186
187 switch (apk_origin) {
188 case APK_ORIGIN_VENDOR:
189 origin_partition = "vendor";
190 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900191 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100192 break;
193 case APK_ORIGIN_PRODUCT:
194 origin_partition = "product";
195 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900196 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100197 break;
198 default:
199 origin_partition = "unknown";
200 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900201 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100202 }
203 library_path = library_path + ":" + origin_lib_path;
204 permitted_path = permitted_path + ":" + origin_lib_path;
205
Justin Yun089c1352020-02-06 16:53:08 +0900206 // Also give access to LLNDK libraries since they are available to vendor or product
207 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100208
209 // Different name is useful for debugging
210 namespace_name = kVendorClassloaderNamespaceName;
211 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
212 origin_partition, library_path.c_str());
213 } else {
214 // extended public libraries are NOT available to vendor apks, otherwise it
215 // would be system->vendor violation.
216 if (!extended_public_libraries().empty()) {
217 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
218 }
219 }
220
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100221 if (is_shared) {
222 // Show in the name that the namespace was created as shared, for debugging
223 // purposes.
224 namespace_name = namespace_name + kSharedNamespaceSuffix;
225 }
226
Orion Hodson9b16e342019-10-09 13:29:16 +0100227 // Create the app namespace
228 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
229 // Heuristic: the first classloader with non-empty library_path is assumed to
230 // be the main classloader for app
231 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
232 // friends) and then passing it down to here.
233 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
234 // Policy: the namespace for the main classloader is also used as the
235 // anonymous namespace.
236 bool also_used_as_anonymous = is_main_classloader;
237 // Note: this function is executed with g_namespaces_mutex held, thus no
238 // racing here.
239 auto app_ns = NativeLoaderNamespace::Create(
240 namespace_name, library_path, permitted_path, parent_ns, is_shared,
241 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900242 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100243 return app_ns.error();
244 }
245 // ... and link to other namespaces to allow access to some public libraries
246 bool is_bridged = app_ns->IsBridged();
247
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000248 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
249 if (!system_ns.ok()) {
250 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100251 }
252
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000253 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900254 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100255 return linked.error();
256 }
257
258 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
259 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900260 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100261 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900262 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100263 return linked.error();
264 }
265 }
266
267 // Give access to NNAPI libraries (apex-updated LLNDK library).
268 auto nnapi_ns =
269 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900270 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100271 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900272 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100273 return linked.error();
274 }
275 }
276
277 // Give access to VNDK-SP libraries from the 'vndk' namespace.
278 if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
279 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900280 if (vndk_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100281 linked = app_ns->Link(*vndk_ns, vndksp_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900282 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100283 return linked.error();
284 }
285 }
286 }
287
Luke Huang5c017722019-12-17 10:54:26 +0800288 // TODO(b/143733063): Remove it after library path of apex module is supported.
289 auto cronet_ns =
290 NativeLoaderNamespace::GetExportedNamespace(kCronetNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900291 if (cronet_ns.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800292 linked = app_ns->Link(*cronet_ns, cronet_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900293 if (!linked.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800294 return linked.error();
295 }
296 }
297
Orion Hodson9b16e342019-10-09 13:29:16 +0100298 if (!vendor_public_libraries().empty()) {
299 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900300 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000301 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900302 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100303 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900304 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100305 return linked.error();
306 }
307 }
308 }
309
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000310 auto& emplaced = namespaces_.emplace_back(
311 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100312 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000313 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100314 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000315 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100316}
317
318NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
319 jobject class_loader) {
320 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
321 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
322 return env->IsSameObject(value.first, class_loader);
323 });
324 if (it != namespaces_.end()) {
325 return &it->second;
326 }
327
328 return nullptr;
329}
330
331NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
332 jobject class_loader) {
333 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
334
335 while (parent_class_loader != nullptr) {
336 NativeLoaderNamespace* ns;
337 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
338 return ns;
339 }
340
341 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
342 }
343
344 return nullptr;
345}
346
347} // namespace android::nativeloader