blob: dfbdefd53d12c384a078331275b9f5d95522d6f3 [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";
Justin Yuneb4f08c2020-02-18 11:29:07 +090045constexpr const char* kVndkProductNamespaceName = "vndk_product";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090046constexpr const char* kArtNamespaceName = "com_android_art";
47constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
48constexpr const char* kCronetNamespaceName = "com_android_cronet";
49constexpr const char* kStatsdNamespaceName = "com_android_os_statsd";
Orion Hodson9b16e342019-10-09 13:29:16 +010050
51// classloader-namespace is a linker namespace that is created for the loaded
52// app. To be specific, it is created for the app classloader. When
53// System.load() is called from a Java class that is loaded from the
54// classloader, the classloader-namespace namespace associated with that
55// classloader is selected for dlopen. The namespace is configured so that its
56// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090057// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010058// This way an app can only load its own JNI libraries along with the public libs.
59constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
60// Same thing for vendor APKs.
61constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010062// If the namespace is shared then add this suffix to form
63// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
64// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
65// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090066// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010067// access to the platform libraries for apps bundled in the system image,
68// including their later updates installed in /data.
69constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010070
71// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
72// System.load() with an absolute path which is outside of the classloader library search path.
73// This list includes all directories app is allowed to access this way.
74constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
75
76constexpr const char* kVendorLibPath = "/vendor/" LIB;
77constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
78
79const std::regex kVendorDexPathRegex("(^|:)/vendor/");
80const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
81
82// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010083using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010084 APK_ORIGIN_DEFAULT = 0,
85 APK_ORIGIN_VENDOR = 1,
86 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010087};
Orion Hodson9b16e342019-10-09 13:29:16 +010088
89jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
90 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
91 jmethodID get_parent =
92 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
93
94 return env->CallObjectMethod(class_loader, get_parent);
95}
96
97ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
98 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
99
100 if (dex_path != nullptr) {
101 ScopedUtfChars dex_path_utf_chars(env, dex_path);
102
103 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
104 apk_origin = APK_ORIGIN_VENDOR;
105 }
106
107 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
108 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
109 "Dex path contains both vendor and product partition : %s",
110 dex_path_utf_chars.c_str());
111
112 apk_origin = APK_ORIGIN_PRODUCT;
113 }
114 }
115 return apk_origin;
116}
117
118} // namespace
119
120void LibraryNamespaces::Initialize() {
121 // Once public namespace is initialized there is no
122 // point in running this code - it will have no effect
123 // on the current list of public libraries.
124 if (initialized_) {
125 return;
126 }
127
128 // android_init_namespaces() expects all the public libraries
129 // to be loaded so that they can be found by soname alone.
130 //
131 // TODO(dimitry): this is a bit misleading since we do not know
132 // if the vendor public library is going to be opened from /vendor/lib
133 // we might as well end up loading them from /system/lib or /product/lib
134 // For now we rely on CTS test to catch things like this but
135 // it should probably be addressed in the future.
136 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
137 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
138 "Error preloading public library %s: %s", soname.c_str(), dlerror());
139 }
140}
141
142Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
143 jobject class_loader, bool is_shared,
144 jstring dex_path,
145 jstring java_library_path,
146 jstring java_permitted_path) {
147 std::string library_path; // empty string by default.
148
149 if (java_library_path != nullptr) {
150 ScopedUtfChars library_path_utf_chars(env, java_library_path);
151 library_path = library_path_utf_chars.c_str();
152 }
153
154 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
155
156 // (http://b/27588281) This is a workaround for apps using custom
157 // classloaders and calling System.load() with an absolute path which
158 // is outside of the classloader library search path.
159 //
160 // This part effectively allows such a classloader to access anything
161 // under /data and /mnt/expand
162 std::string permitted_path = kWhitelistedDirectories;
163
164 if (java_permitted_path != nullptr) {
165 ScopedUtfChars path(env, java_permitted_path);
166 if (path.c_str() != nullptr && path.size() > 0) {
167 permitted_path = permitted_path + ":" + path.c_str();
168 }
169 }
170
171 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
172 "There is already a namespace associated with this classloader");
173
174 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100175 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900176 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100177 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900178 (apk_origin == APK_ORIGIN_PRODUCT &&
179 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100180 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900181 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100182 // For vendor / product apks, give access to the vendor / product lib even though
183 // they are treated as unbundled; the libs and apks are still bundled
184 // together in the vendor / product partition.
185 const char* origin_partition;
186 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900187 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100188
189 switch (apk_origin) {
190 case APK_ORIGIN_VENDOR:
191 origin_partition = "vendor";
192 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900193 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100194 break;
195 case APK_ORIGIN_PRODUCT:
196 origin_partition = "product";
197 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900198 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100199 break;
200 default:
201 origin_partition = "unknown";
202 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900203 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100204 }
205 library_path = library_path + ":" + origin_lib_path;
206 permitted_path = permitted_path + ":" + origin_lib_path;
207
Justin Yun089c1352020-02-06 16:53:08 +0900208 // Also give access to LLNDK libraries since they are available to vendor or product
209 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100210
211 // Different name is useful for debugging
212 namespace_name = kVendorClassloaderNamespaceName;
213 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
214 origin_partition, library_path.c_str());
215 } else {
216 // extended public libraries are NOT available to vendor apks, otherwise it
217 // would be system->vendor violation.
218 if (!extended_public_libraries().empty()) {
219 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
220 }
221 }
222
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100223 if (is_shared) {
224 // Show in the name that the namespace was created as shared, for debugging
225 // purposes.
226 namespace_name = namespace_name + kSharedNamespaceSuffix;
227 }
228
Orion Hodson9b16e342019-10-09 13:29:16 +0100229 // Create the app namespace
230 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
231 // Heuristic: the first classloader with non-empty library_path is assumed to
232 // be the main classloader for app
233 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
234 // friends) and then passing it down to here.
235 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
236 // Policy: the namespace for the main classloader is also used as the
237 // anonymous namespace.
238 bool also_used_as_anonymous = is_main_classloader;
239 // Note: this function is executed with g_namespaces_mutex held, thus no
240 // racing here.
241 auto app_ns = NativeLoaderNamespace::Create(
242 namespace_name, library_path, permitted_path, parent_ns, is_shared,
243 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900244 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100245 return app_ns.error();
246 }
247 // ... and link to other namespaces to allow access to some public libraries
248 bool is_bridged = app_ns->IsBridged();
249
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000250 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
251 if (!system_ns.ok()) {
252 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100253 }
254
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000255 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900256 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100257 return linked.error();
258 }
259
260 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
261 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900262 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100263 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900264 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100265 return linked.error();
266 }
267 }
268
269 // Give access to NNAPI libraries (apex-updated LLNDK library).
270 auto nnapi_ns =
271 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900272 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100273 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900274 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100275 return linked.error();
276 }
277 }
278
Justin Yuneb4f08c2020-02-18 11:29:07 +0900279 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
280 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100281 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900282 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900283 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
284 if (!linked.ok()) {
285 return linked.error();
286 }
287 }
288 }
289
290 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
291 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
292 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
293 if (vndk_ns.ok()) {
294 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900295 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100296 return linked.error();
297 }
298 }
299 }
300
Luke Huang5c017722019-12-17 10:54:26 +0800301 // TODO(b/143733063): Remove it after library path of apex module is supported.
302 auto cronet_ns =
303 NativeLoaderNamespace::GetExportedNamespace(kCronetNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900304 if (cronet_ns.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800305 linked = app_ns->Link(*cronet_ns, cronet_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900306 if (!linked.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800307 return linked.error();
308 }
309 }
310
Jeffrey Huang52575032020-02-11 17:33:45 -0800311 // Give access to StatsdAPI libraries
312 auto statsd_ns =
313 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
314 if (statsd_ns.ok()) {
315 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
316 if (!linked.ok()) {
317 return linked.error();
318 }
319 }
320
Orion Hodson9b16e342019-10-09 13:29:16 +0100321 if (!vendor_public_libraries().empty()) {
322 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900323 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000324 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900325 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100326 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900327 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100328 return linked.error();
329 }
330 }
331 }
332
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000333 auto& emplaced = namespaces_.emplace_back(
334 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100335 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000336 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100337 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000338 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100339}
340
341NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
342 jobject class_loader) {
343 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
344 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
345 return env->IsSameObject(value.first, class_loader);
346 });
347 if (it != namespaces_.end()) {
348 return &it->second;
349 }
350
351 return nullptr;
352}
353
354NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
355 jobject class_loader) {
356 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
357
358 while (parent_class_loader != nullptr) {
359 NativeLoaderNamespace* ns;
360 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
361 return ns;
362 }
363
364 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
365 }
366
367 return nullptr;
368}
369
370} // namespace android::nativeloader