blob: 9c658d78e52142775c4943c37377a3aa555c9e9d [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";
Jeffrey Huang52575032020-02-11 17:33:45 -080048constexpr const char* kStatsdNamespaceName = "com.android.os.statsd";
Orion Hodson9b16e342019-10-09 13:29:16 +010049
50// classloader-namespace is a linker namespace that is created for the loaded
51// app. To be specific, it is created for the app classloader. When
52// System.load() is called from a Java class that is loaded from the
53// classloader, the classloader-namespace namespace associated with that
54// classloader is selected for dlopen. The namespace is configured so that its
55// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090056// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010057// This way an app can only load its own JNI libraries along with the public libs.
58constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
59// Same thing for vendor APKs.
60constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010061// If the namespace is shared then add this suffix to form
62// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
63// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
64// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090065// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010066// access to the platform libraries for apps bundled in the system image,
67// including their later updates installed in /data.
68constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010069
70// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
71// System.load() with an absolute path which is outside of the classloader library search path.
72// This list includes all directories app is allowed to access this way.
73constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
74
75constexpr const char* kVendorLibPath = "/vendor/" LIB;
76constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
77
78const std::regex kVendorDexPathRegex("(^|:)/vendor/");
79const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
80
81// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010082using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010083 APK_ORIGIN_DEFAULT = 0,
84 APK_ORIGIN_VENDOR = 1,
85 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010086};
Orion Hodson9b16e342019-10-09 13:29:16 +010087
88jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
89 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
90 jmethodID get_parent =
91 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
92
93 return env->CallObjectMethod(class_loader, get_parent);
94}
95
96ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
97 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
98
99 if (dex_path != nullptr) {
100 ScopedUtfChars dex_path_utf_chars(env, dex_path);
101
102 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
103 apk_origin = APK_ORIGIN_VENDOR;
104 }
105
106 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
107 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
108 "Dex path contains both vendor and product partition : %s",
109 dex_path_utf_chars.c_str());
110
111 apk_origin = APK_ORIGIN_PRODUCT;
112 }
113 }
114 return apk_origin;
115}
116
117} // namespace
118
119void LibraryNamespaces::Initialize() {
120 // Once public namespace is initialized there is no
121 // point in running this code - it will have no effect
122 // on the current list of public libraries.
123 if (initialized_) {
124 return;
125 }
126
127 // android_init_namespaces() expects all the public libraries
128 // to be loaded so that they can be found by soname alone.
129 //
130 // TODO(dimitry): this is a bit misleading since we do not know
131 // if the vendor public library is going to be opened from /vendor/lib
132 // we might as well end up loading them from /system/lib or /product/lib
133 // For now we rely on CTS test to catch things like this but
134 // it should probably be addressed in the future.
135 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
136 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
137 "Error preloading public library %s: %s", soname.c_str(), dlerror());
138 }
139}
140
141Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
142 jobject class_loader, bool is_shared,
143 jstring dex_path,
144 jstring java_library_path,
145 jstring java_permitted_path) {
146 std::string library_path; // empty string by default.
147
148 if (java_library_path != nullptr) {
149 ScopedUtfChars library_path_utf_chars(env, java_library_path);
150 library_path = library_path_utf_chars.c_str();
151 }
152
153 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
154
155 // (http://b/27588281) This is a workaround for apps using custom
156 // classloaders and calling System.load() with an absolute path which
157 // is outside of the classloader library search path.
158 //
159 // This part effectively allows such a classloader to access anything
160 // under /data and /mnt/expand
161 std::string permitted_path = kWhitelistedDirectories;
162
163 if (java_permitted_path != nullptr) {
164 ScopedUtfChars path(env, java_permitted_path);
165 if (path.c_str() != nullptr && path.size() > 0) {
166 permitted_path = permitted_path + ":" + path.c_str();
167 }
168 }
169
170 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
171 "There is already a namespace associated with this classloader");
172
173 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100174 std::string namespace_name = kClassloaderNamespaceName;
Orion Hodson9b16e342019-10-09 13:29:16 +0100175 bool unbundled_vendor_or_product_app = false;
176 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900177 (apk_origin == APK_ORIGIN_PRODUCT &&
178 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100179 !is_shared) {
180 unbundled_vendor_or_product_app = true;
181 // For vendor / product apks, give access to the vendor / product lib even though
182 // they are treated as unbundled; the libs and apks are still bundled
183 // together in the vendor / product partition.
184 const char* origin_partition;
185 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900186 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100187
188 switch (apk_origin) {
189 case APK_ORIGIN_VENDOR:
190 origin_partition = "vendor";
191 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900192 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100193 break;
194 case APK_ORIGIN_PRODUCT:
195 origin_partition = "product";
196 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900197 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100198 break;
199 default:
200 origin_partition = "unknown";
201 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900202 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100203 }
204 library_path = library_path + ":" + origin_lib_path;
205 permitted_path = permitted_path + ":" + origin_lib_path;
206
Justin Yun089c1352020-02-06 16:53:08 +0900207 // Also give access to LLNDK libraries since they are available to vendor or product
208 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100209
210 // Different name is useful for debugging
211 namespace_name = kVendorClassloaderNamespaceName;
212 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
213 origin_partition, library_path.c_str());
214 } else {
215 // extended public libraries are NOT available to vendor apks, otherwise it
216 // would be system->vendor violation.
217 if (!extended_public_libraries().empty()) {
218 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
219 }
220 }
221
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100222 if (is_shared) {
223 // Show in the name that the namespace was created as shared, for debugging
224 // purposes.
225 namespace_name = namespace_name + kSharedNamespaceSuffix;
226 }
227
Orion Hodson9b16e342019-10-09 13:29:16 +0100228 // Create the app namespace
229 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
230 // Heuristic: the first classloader with non-empty library_path is assumed to
231 // be the main classloader for app
232 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
233 // friends) and then passing it down to here.
234 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
235 // Policy: the namespace for the main classloader is also used as the
236 // anonymous namespace.
237 bool also_used_as_anonymous = is_main_classloader;
238 // Note: this function is executed with g_namespaces_mutex held, thus no
239 // racing here.
240 auto app_ns = NativeLoaderNamespace::Create(
241 namespace_name, library_path, permitted_path, parent_ns, is_shared,
242 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900243 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100244 return app_ns.error();
245 }
246 // ... and link to other namespaces to allow access to some public libraries
247 bool is_bridged = app_ns->IsBridged();
248
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000249 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
250 if (!system_ns.ok()) {
251 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100252 }
253
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000254 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900255 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100256 return linked.error();
257 }
258
259 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
260 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900261 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100262 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900263 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100264 return linked.error();
265 }
266 }
267
268 // Give access to NNAPI libraries (apex-updated LLNDK library).
269 auto nnapi_ns =
270 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900271 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100272 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900273 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100274 return linked.error();
275 }
276 }
277
278 // Give access to VNDK-SP libraries from the 'vndk' namespace.
279 if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
280 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900281 if (vndk_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100282 linked = app_ns->Link(*vndk_ns, vndksp_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900283 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100284 return linked.error();
285 }
286 }
287 }
288
Luke Huang5c017722019-12-17 10:54:26 +0800289 // TODO(b/143733063): Remove it after library path of apex module is supported.
290 auto cronet_ns =
291 NativeLoaderNamespace::GetExportedNamespace(kCronetNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900292 if (cronet_ns.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800293 linked = app_ns->Link(*cronet_ns, cronet_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900294 if (!linked.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800295 return linked.error();
296 }
297 }
298
Jeffrey Huang52575032020-02-11 17:33:45 -0800299 // Give access to StatsdAPI libraries
300 auto statsd_ns =
301 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
302 if (statsd_ns.ok()) {
303 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
304 if (!linked.ok()) {
305 return linked.error();
306 }
307 }
308
Orion Hodson9b16e342019-10-09 13:29:16 +0100309 if (!vendor_public_libraries().empty()) {
310 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900311 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000312 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900313 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100314 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900315 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100316 return linked.error();
317 }
318 }
319 }
320
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000321 auto& emplaced = namespaces_.emplace_back(
322 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100323 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000324 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100325 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000326 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100327}
328
329NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
330 jobject class_loader) {
331 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
332 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
333 return env->IsSameObject(value.first, class_loader);
334 });
335 if (it != namespaces_.end()) {
336 return &it->second;
337 }
338
339 return nullptr;
340}
341
342NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
343 jobject class_loader) {
344 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
345
346 while (parent_class_loader != nullptr) {
347 NativeLoaderNamespace* ns;
348 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
349 return ns;
350 }
351
352 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
353 }
354
355 return nullptr;
356}
357
358} // namespace android::nativeloader