blob: 14ba7214206a133dae93e51ded1f5c83c608b363 [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";
Victor Changd20e51d2020-05-05 16:01:19 +010054constexpr const char* kI18nNamespaceName = "com_android_i18n";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090055constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090056constexpr const char* kStatsdNamespaceName = "com_android_os_statsd";
Orion Hodson9b16e342019-10-09 13:29:16 +010057
58// classloader-namespace is a linker namespace that is created for the loaded
59// app. To be specific, it is created for the app classloader. When
60// System.load() is called from a Java class that is loaded from the
61// classloader, the classloader-namespace namespace associated with that
62// classloader is selected for dlopen. The namespace is configured so that its
63// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090064// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010065// This way an app can only load its own JNI libraries along with the public libs.
66constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
67// Same thing for vendor APKs.
68constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010069// If the namespace is shared then add this suffix to form
70// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
71// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
72// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090073// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010074// access to the platform libraries for apps bundled in the system image,
75// including their later updates installed in /data.
76constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010077
78// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
79// System.load() with an absolute path which is outside of the classloader library search path.
80// This list includes all directories app is allowed to access this way.
81constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
82
83constexpr const char* kVendorLibPath = "/vendor/" LIB;
84constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
85
86const std::regex kVendorDexPathRegex("(^|:)/vendor/");
87const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
88
89// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010090using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010091 APK_ORIGIN_DEFAULT = 0,
92 APK_ORIGIN_VENDOR = 1,
93 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010094};
Orion Hodson9b16e342019-10-09 13:29:16 +010095
96jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
97 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
98 jmethodID get_parent =
99 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
100
101 return env->CallObjectMethod(class_loader, get_parent);
102}
103
Jooyung Han538f99a2020-03-03 00:46:50 +0900104ApkOrigin GetApkOriginFromDexPath(const std::string& dex_path) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100105 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
Jooyung Han538f99a2020-03-03 00:46:50 +0900106 if (std::regex_search(dex_path, kVendorDexPathRegex)) {
107 apk_origin = APK_ORIGIN_VENDOR;
108 }
109 if (std::regex_search(dex_path, kProductDexPathRegex)) {
110 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
111 "Dex path contains both vendor and product partition : %s",
112 dex_path.c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100113
Jooyung Han538f99a2020-03-03 00:46:50 +0900114 apk_origin = APK_ORIGIN_PRODUCT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100115 }
116 return apk_origin;
117}
118
119} // namespace
120
121void LibraryNamespaces::Initialize() {
122 // Once public namespace is initialized there is no
123 // point in running this code - it will have no effect
124 // on the current list of public libraries.
125 if (initialized_) {
126 return;
127 }
128
129 // android_init_namespaces() expects all the public libraries
130 // to be loaded so that they can be found by soname alone.
131 //
132 // TODO(dimitry): this is a bit misleading since we do not know
133 // if the vendor public library is going to be opened from /vendor/lib
134 // we might as well end up loading them from /system/lib or /product/lib
135 // For now we rely on CTS test to catch things like this but
136 // it should probably be addressed in the future.
137 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
138 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
139 "Error preloading public library %s: %s", soname.c_str(), dlerror());
140 }
141}
142
143Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
144 jobject class_loader, bool is_shared,
Jooyung Han538f99a2020-03-03 00:46:50 +0900145 jstring dex_path_j,
Orion Hodson9b16e342019-10-09 13:29:16 +0100146 jstring java_library_path,
147 jstring java_permitted_path) {
148 std::string library_path; // empty string by default.
Jooyung Han538f99a2020-03-03 00:46:50 +0900149 std::string dex_path;
Orion Hodson9b16e342019-10-09 13:29:16 +0100150
151 if (java_library_path != nullptr) {
152 ScopedUtfChars library_path_utf_chars(env, java_library_path);
153 library_path = library_path_utf_chars.c_str();
154 }
155
Jooyung Han538f99a2020-03-03 00:46:50 +0900156 if (dex_path_j != nullptr) {
157 ScopedUtfChars dex_path_chars(env, dex_path_j);
158 dex_path = dex_path_chars.c_str();
159 }
160
161 ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path);
Orion Hodson9b16e342019-10-09 13:29:16 +0100162
163 // (http://b/27588281) This is a workaround for apps using custom
164 // classloaders and calling System.load() with an absolute path which
165 // is outside of the classloader library search path.
166 //
167 // This part effectively allows such a classloader to access anything
168 // under /data and /mnt/expand
169 std::string permitted_path = kWhitelistedDirectories;
170
171 if (java_permitted_path != nullptr) {
172 ScopedUtfChars path(env, java_permitted_path);
173 if (path.c_str() != nullptr && path.size() > 0) {
174 permitted_path = permitted_path + ":" + path.c_str();
175 }
176 }
177
178 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
179 "There is already a namespace associated with this classloader");
180
181 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100182 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900183 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100184 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900185 (apk_origin == APK_ORIGIN_PRODUCT &&
186 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100187 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900188 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100189 // For vendor / product apks, give access to the vendor / product lib even though
190 // they are treated as unbundled; the libs and apks are still bundled
191 // together in the vendor / product partition.
192 const char* origin_partition;
193 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900194 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100195
196 switch (apk_origin) {
197 case APK_ORIGIN_VENDOR:
198 origin_partition = "vendor";
199 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900200 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100201 break;
202 case APK_ORIGIN_PRODUCT:
203 origin_partition = "product";
204 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900205 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100206 break;
207 default:
208 origin_partition = "unknown";
209 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900210 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100211 }
212 library_path = library_path + ":" + origin_lib_path;
213 permitted_path = permitted_path + ":" + origin_lib_path;
214
Justin Yun089c1352020-02-06 16:53:08 +0900215 // Also give access to LLNDK libraries since they are available to vendor or product
216 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100217
218 // Different name is useful for debugging
219 namespace_name = kVendorClassloaderNamespaceName;
220 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
221 origin_partition, library_path.c_str());
222 } else {
223 // extended public libraries are NOT available to vendor apks, otherwise it
224 // would be system->vendor violation.
225 if (!extended_public_libraries().empty()) {
226 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
227 }
228 }
229
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100230 if (is_shared) {
231 // Show in the name that the namespace was created as shared, for debugging
232 // purposes.
233 namespace_name = namespace_name + kSharedNamespaceSuffix;
234 }
235
Orion Hodson9b16e342019-10-09 13:29:16 +0100236 // Create the app namespace
237 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
238 // Heuristic: the first classloader with non-empty library_path is assumed to
239 // be the main classloader for app
240 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
241 // friends) and then passing it down to here.
242 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
243 // Policy: the namespace for the main classloader is also used as the
244 // anonymous namespace.
245 bool also_used_as_anonymous = is_main_classloader;
246 // Note: this function is executed with g_namespaces_mutex held, thus no
247 // racing here.
248 auto app_ns = NativeLoaderNamespace::Create(
249 namespace_name, library_path, permitted_path, parent_ns, is_shared,
250 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900251 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100252 return app_ns.error();
253 }
254 // ... and link to other namespaces to allow access to some public libraries
255 bool is_bridged = app_ns->IsBridged();
256
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000257 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
258 if (!system_ns.ok()) {
259 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100260 }
261
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000262 auto linked = app_ns->Link(*system_ns, system_exposed_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 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
268 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900269 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100270 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900271 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100272 return linked.error();
273 }
274 }
275
Victor Changd20e51d2020-05-05 16:01:19 +0100276 auto i18n_ns = NativeLoaderNamespace::GetExportedNamespace(kI18nNamespaceName, is_bridged);
277 // i18n APEX does not exist on host, and under certain build conditions.
278 if (i18n_ns.ok()) {
279 linked = app_ns->Link(*i18n_ns, i18n_public_libraries());
280 if (!linked.ok()) {
281 return linked.error();
282 }
283 }
284
Orion Hodson9b16e342019-10-09 13:29:16 +0100285 // Give access to NNAPI libraries (apex-updated LLNDK library).
286 auto nnapi_ns =
287 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900288 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100289 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900290 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100291 return linked.error();
292 }
293 }
294
Justin Yuneb4f08c2020-02-18 11:29:07 +0900295 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
296 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100297 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900298 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900299 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
300 if (!linked.ok()) {
301 return linked.error();
302 }
303 }
304 }
305
306 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
307 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
308 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
309 if (vndk_ns.ok()) {
310 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900311 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100312 return linked.error();
313 }
314 }
315 }
316
Jooyung Han538f99a2020-03-03 00:46:50 +0900317 auto apex_ns_name = FindApexNamespaceName(dex_path);
318 if (apex_ns_name.ok()) {
319 const auto& jni_libs = apex_jni_libraries(*apex_ns_name);
320 if (jni_libs != "") {
321 auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
322 if (apex_ns.ok()) {
323 auto link = app_ns->Link(*apex_ns, jni_libs);
324 if (!link.ok()) {
325 return linked.error();
326 }
327 }
328 }
329 }
330
Jeffrey Huang52575032020-02-11 17:33:45 -0800331 // Give access to StatsdAPI libraries
332 auto statsd_ns =
333 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
334 if (statsd_ns.ok()) {
335 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
336 if (!linked.ok()) {
337 return linked.error();
338 }
339 }
340
Orion Hodson9b16e342019-10-09 13:29:16 +0100341 if (!vendor_public_libraries().empty()) {
342 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900343 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000344 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900345 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100346 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900347 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100348 return linked.error();
349 }
350 }
351 }
352
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000353 auto& emplaced = namespaces_.emplace_back(
354 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100355 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000356 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100357 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000358 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100359}
360
361NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
362 jobject class_loader) {
363 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
364 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
365 return env->IsSameObject(value.first, class_loader);
366 });
367 if (it != namespaces_.end()) {
368 return &it->second;
369 }
370
371 return nullptr;
372}
373
374NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
375 jobject class_loader) {
376 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
377
378 while (parent_class_loader != nullptr) {
379 NativeLoaderNamespace* ns;
380 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
381 return ns;
382 }
383
384 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
385 }
386
387 return nullptr;
388}
389
Jooyung Han538f99a2020-03-03 00:46:50 +0900390base::Result<std::string> FindApexNamespaceName(const std::string& location) {
391 // Lots of implicit assumptions here: we expect `location` to be of the form:
392 // /apex/modulename/...
393 //
394 // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it.
395 if (android::base::StartsWith(location, kApexPath)) {
396 size_t start_index = strlen(kApexPath);
397 size_t slash_index = location.find_first_of('/', start_index);
398 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
399 "Error finding namespace of apex: no slash in path %s", location.c_str());
400 std::string name = location.substr(start_index, slash_index - start_index);
401 std::replace(name.begin(), name.end(), '.', '_');
402 return name;
403 }
404 return base::Error();
405}
406
Orion Hodson9b16e342019-10-09 13:29:16 +0100407} // namespace android::nativeloader
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100408
409#endif // defined(ART_TARGET_ANDROID)