blob: d42a4b55ed5fe6d8dd5f9bdceb929430c148f2db [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 */
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010016
17#if defined(ART_TARGET_ANDROID)
18
Orion Hodson9b16e342019-10-09 13:29:16 +010019#include "library_namespaces.h"
20
21#include <dirent.h>
22#include <dlfcn.h>
23
24#include <regex>
25#include <string>
26#include <vector>
27
28#include <android-base/file.h>
29#include <android-base/logging.h>
30#include <android-base/macros.h>
31#include <android-base/properties.h>
Jooyung Han538f99a2020-03-03 00:46:50 +090032#include <android-base/result.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010033#include <android-base/strings.h>
Orion Hodson6dc0a432020-02-06 14:28:28 +000034#include <nativehelper/scoped_utf_chars.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010035
36#include "nativeloader/dlext_namespaces.h"
37#include "public_libraries.h"
38#include "utils.h"
39
Orion Hodson9b16e342019-10-09 13:29:16 +010040namespace android::nativeloader {
41
42namespace {
Jooyung Han538f99a2020-03-03 00:46:50 +090043
44constexpr const char* kApexPath = "/apex/";
45
Orion Hodson9b16e342019-10-09 13:29:16 +010046// The device may be configured to have the vendor libraries loaded to a separate namespace.
47// For historical reasons this namespace was named sphal but effectively it is intended
48// to use to load vendor libraries to separate namespace with controlled interface between
49// vendor and system namespaces.
50constexpr const char* kVendorNamespaceName = "sphal";
51constexpr const char* kVndkNamespaceName = "vndk";
Justin Yuneb4f08c2020-02-18 11:29:07 +090052constexpr const char* kVndkProductNamespaceName = "vndk_product";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090053constexpr const char* kArtNamespaceName = "com_android_art";
54constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090055constexpr const char* kStatsdNamespaceName = "com_android_os_statsd";
Orion Hodson9b16e342019-10-09 13:29:16 +010056
57// classloader-namespace is a linker namespace that is created for the loaded
58// app. To be specific, it is created for the app classloader. When
59// System.load() is called from a Java class that is loaded from the
60// classloader, the classloader-namespace namespace associated with that
61// classloader is selected for dlopen. The namespace is configured so that its
62// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090063// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010064// This way an app can only load its own JNI libraries along with the public libs.
65constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
66// Same thing for vendor APKs.
67constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010068// If the namespace is shared then add this suffix to form
69// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
70// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
71// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090072// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010073// access to the platform libraries for apps bundled in the system image,
74// including their later updates installed in /data.
75constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010076
77// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
78// System.load() with an absolute path which is outside of the classloader library search path.
79// This list includes all directories app is allowed to access this way.
80constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
81
82constexpr const char* kVendorLibPath = "/vendor/" LIB;
83constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
84
85const std::regex kVendorDexPathRegex("(^|:)/vendor/");
86const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
87
88// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010089using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010090 APK_ORIGIN_DEFAULT = 0,
91 APK_ORIGIN_VENDOR = 1,
92 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010093};
Orion Hodson9b16e342019-10-09 13:29:16 +010094
95jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
96 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
97 jmethodID get_parent =
98 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
99
100 return env->CallObjectMethod(class_loader, get_parent);
101}
102
Jooyung Han538f99a2020-03-03 00:46:50 +0900103ApkOrigin GetApkOriginFromDexPath(const std::string& dex_path) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100104 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
Jooyung Han538f99a2020-03-03 00:46:50 +0900105 if (std::regex_search(dex_path, kVendorDexPathRegex)) {
106 apk_origin = APK_ORIGIN_VENDOR;
107 }
108 if (std::regex_search(dex_path, kProductDexPathRegex)) {
109 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
110 "Dex path contains both vendor and product partition : %s",
111 dex_path.c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100112
Jooyung Han538f99a2020-03-03 00:46:50 +0900113 apk_origin = APK_ORIGIN_PRODUCT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100114 }
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,
Jooyung Han538f99a2020-03-03 00:46:50 +0900144 jstring dex_path_j,
Orion Hodson9b16e342019-10-09 13:29:16 +0100145 jstring java_library_path,
146 jstring java_permitted_path) {
147 std::string library_path; // empty string by default.
Jooyung Han538f99a2020-03-03 00:46:50 +0900148 std::string dex_path;
Orion Hodson9b16e342019-10-09 13:29:16 +0100149
150 if (java_library_path != nullptr) {
151 ScopedUtfChars library_path_utf_chars(env, java_library_path);
152 library_path = library_path_utf_chars.c_str();
153 }
154
Jooyung Han538f99a2020-03-03 00:46:50 +0900155 if (dex_path_j != nullptr) {
156 ScopedUtfChars dex_path_chars(env, dex_path_j);
157 dex_path = dex_path_chars.c_str();
158 }
159
160 ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path);
Orion Hodson9b16e342019-10-09 13:29:16 +0100161
162 // (http://b/27588281) This is a workaround for apps using custom
163 // classloaders and calling System.load() with an absolute path which
164 // is outside of the classloader library search path.
165 //
166 // This part effectively allows such a classloader to access anything
167 // under /data and /mnt/expand
168 std::string permitted_path = kWhitelistedDirectories;
169
170 if (java_permitted_path != nullptr) {
171 ScopedUtfChars path(env, java_permitted_path);
172 if (path.c_str() != nullptr && path.size() > 0) {
173 permitted_path = permitted_path + ":" + path.c_str();
174 }
175 }
176
177 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
178 "There is already a namespace associated with this classloader");
179
180 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100181 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900182 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100183 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900184 (apk_origin == APK_ORIGIN_PRODUCT &&
185 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100186 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900187 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100188 // For vendor / product apks, give access to the vendor / product lib even though
189 // they are treated as unbundled; the libs and apks are still bundled
190 // together in the vendor / product partition.
191 const char* origin_partition;
192 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900193 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100194
195 switch (apk_origin) {
196 case APK_ORIGIN_VENDOR:
197 origin_partition = "vendor";
198 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900199 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100200 break;
201 case APK_ORIGIN_PRODUCT:
202 origin_partition = "product";
203 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900204 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100205 break;
206 default:
207 origin_partition = "unknown";
208 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900209 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100210 }
211 library_path = library_path + ":" + origin_lib_path;
212 permitted_path = permitted_path + ":" + origin_lib_path;
213
Justin Yun089c1352020-02-06 16:53:08 +0900214 // Also give access to LLNDK libraries since they are available to vendor or product
215 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100216
217 // Different name is useful for debugging
218 namespace_name = kVendorClassloaderNamespaceName;
219 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
220 origin_partition, library_path.c_str());
221 } else {
222 // extended public libraries are NOT available to vendor apks, otherwise it
223 // would be system->vendor violation.
224 if (!extended_public_libraries().empty()) {
225 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
226 }
227 }
228
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100229 if (is_shared) {
230 // Show in the name that the namespace was created as shared, for debugging
231 // purposes.
232 namespace_name = namespace_name + kSharedNamespaceSuffix;
233 }
234
Orion Hodson9b16e342019-10-09 13:29:16 +0100235 // Create the app namespace
236 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
237 // Heuristic: the first classloader with non-empty library_path is assumed to
238 // be the main classloader for app
239 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
240 // friends) and then passing it down to here.
241 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
242 // Policy: the namespace for the main classloader is also used as the
243 // anonymous namespace.
244 bool also_used_as_anonymous = is_main_classloader;
245 // Note: this function is executed with g_namespaces_mutex held, thus no
246 // racing here.
247 auto app_ns = NativeLoaderNamespace::Create(
248 namespace_name, library_path, permitted_path, parent_ns, is_shared,
249 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900250 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100251 return app_ns.error();
252 }
253 // ... and link to other namespaces to allow access to some public libraries
254 bool is_bridged = app_ns->IsBridged();
255
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000256 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
257 if (!system_ns.ok()) {
258 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100259 }
260
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000261 auto linked = app_ns->Link(*system_ns, system_exposed_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 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
267 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900268 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100269 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900270 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100271 return linked.error();
272 }
273 }
274
275 // Give access to NNAPI libraries (apex-updated LLNDK library).
276 auto nnapi_ns =
277 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900278 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100279 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900280 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100281 return linked.error();
282 }
283 }
284
Justin Yuneb4f08c2020-02-18 11:29:07 +0900285 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
286 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100287 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900288 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900289 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
290 if (!linked.ok()) {
291 return linked.error();
292 }
293 }
294 }
295
296 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
297 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
298 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
299 if (vndk_ns.ok()) {
300 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900301 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100302 return linked.error();
303 }
304 }
305 }
306
Jooyung Han538f99a2020-03-03 00:46:50 +0900307 auto apex_ns_name = FindApexNamespaceName(dex_path);
308 if (apex_ns_name.ok()) {
309 const auto& jni_libs = apex_jni_libraries(*apex_ns_name);
310 if (jni_libs != "") {
311 auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
312 if (apex_ns.ok()) {
313 auto link = app_ns->Link(*apex_ns, jni_libs);
314 if (!link.ok()) {
315 return linked.error();
316 }
317 }
318 }
319 }
320
Jeffrey Huang52575032020-02-11 17:33:45 -0800321 // Give access to StatsdAPI libraries
322 auto statsd_ns =
323 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
324 if (statsd_ns.ok()) {
325 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
326 if (!linked.ok()) {
327 return linked.error();
328 }
329 }
330
Orion Hodson9b16e342019-10-09 13:29:16 +0100331 if (!vendor_public_libraries().empty()) {
332 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900333 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000334 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900335 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100336 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900337 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100338 return linked.error();
339 }
340 }
341 }
342
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000343 auto& emplaced = namespaces_.emplace_back(
344 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100345 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000346 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100347 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000348 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100349}
350
351NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
352 jobject class_loader) {
353 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
354 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
355 return env->IsSameObject(value.first, class_loader);
356 });
357 if (it != namespaces_.end()) {
358 return &it->second;
359 }
360
361 return nullptr;
362}
363
364NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
365 jobject class_loader) {
366 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
367
368 while (parent_class_loader != nullptr) {
369 NativeLoaderNamespace* ns;
370 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
371 return ns;
372 }
373
374 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
375 }
376
377 return nullptr;
378}
379
Jooyung Han538f99a2020-03-03 00:46:50 +0900380base::Result<std::string> FindApexNamespaceName(const std::string& location) {
381 // Lots of implicit assumptions here: we expect `location` to be of the form:
382 // /apex/modulename/...
383 //
384 // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it.
385 if (android::base::StartsWith(location, kApexPath)) {
386 size_t start_index = strlen(kApexPath);
387 size_t slash_index = location.find_first_of('/', start_index);
388 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
389 "Error finding namespace of apex: no slash in path %s", location.c_str());
390 std::string name = location.substr(start_index, slash_index - start_index);
391 std::replace(name.begin(), name.end(), '.', '_');
392 return name;
393 }
394 return base::Error();
395}
396
Orion Hodson9b16e342019-10-09 13:29:16 +0100397} // namespace android::nativeloader
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100398
399#endif // defined(ART_TARGET_ANDROID)