blob: dcd2f657b1351f3045f3b17cd5c5fffc88162f4a [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";
Orion Hodson9b16e342019-10-09 13:29:16 +010053
54// classloader-namespace is a linker namespace that is created for the loaded
55// app. To be specific, it is created for the app classloader. When
56// System.load() is called from a Java class that is loaded from the
57// classloader, the classloader-namespace namespace associated with that
58// classloader is selected for dlopen. The namespace is configured so that its
59// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090060// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010061// This way an app can only load its own JNI libraries along with the public libs.
62constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
63// Same thing for vendor APKs.
64constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010065// If the namespace is shared then add this suffix to form
66// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
67// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
68// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090069// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010070// access to the platform libraries for apps bundled in the system image,
71// including their later updates installed in /data.
72constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010073
74// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
75// System.load() with an absolute path which is outside of the classloader library search path.
76// This list includes all directories app is allowed to access this way.
77constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
78
79constexpr const char* kVendorLibPath = "/vendor/" LIB;
80constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
81
82const std::regex kVendorDexPathRegex("(^|:)/vendor/");
83const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
84
85// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010086using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010087 APK_ORIGIN_DEFAULT = 0,
88 APK_ORIGIN_VENDOR = 1,
89 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010090};
Orion Hodson9b16e342019-10-09 13:29:16 +010091
92jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
93 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
94 jmethodID get_parent =
95 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
96
97 return env->CallObjectMethod(class_loader, get_parent);
98}
99
Jooyung Han538f99a2020-03-03 00:46:50 +0900100ApkOrigin GetApkOriginFromDexPath(const std::string& dex_path) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100101 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
Jooyung Han538f99a2020-03-03 00:46:50 +0900102 if (std::regex_search(dex_path, kVendorDexPathRegex)) {
103 apk_origin = APK_ORIGIN_VENDOR;
104 }
105 if (std::regex_search(dex_path, kProductDexPathRegex)) {
106 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
107 "Dex path contains both vendor and product partition : %s",
108 dex_path.c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100109
Jooyung Han538f99a2020-03-03 00:46:50 +0900110 apk_origin = APK_ORIGIN_PRODUCT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100111 }
112 return apk_origin;
113}
114
115} // namespace
116
117void LibraryNamespaces::Initialize() {
118 // Once public namespace is initialized there is no
119 // point in running this code - it will have no effect
120 // on the current list of public libraries.
121 if (initialized_) {
122 return;
123 }
124
125 // android_init_namespaces() expects all the public libraries
126 // to be loaded so that they can be found by soname alone.
127 //
128 // TODO(dimitry): this is a bit misleading since we do not know
129 // if the vendor public library is going to be opened from /vendor/lib
130 // we might as well end up loading them from /system/lib or /product/lib
131 // For now we rely on CTS test to catch things like this but
132 // it should probably be addressed in the future.
133 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
134 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
135 "Error preloading public library %s: %s", soname.c_str(), dlerror());
136 }
137}
138
Jiyong Park14626a72020-07-02 23:17:58 +0900139// "ALL" is a magic name that allows all public libraries even when the
140// target SDK is > 30. Currently this is used for (Java) shared libraries
141// which don't use <uses-native-library>
142// TODO(b/142191088) remove this hack
143static constexpr const char LIBRARY_ALL[] = "ALL";
144
145// Returns the colon-separated list of library names by filtering uses_libraries from
146// public_libraries. The returned names will actually be available to the app. If the app is pre-S
147// (<= 30), the filtering is not done; the entire public_libraries are provided.
148static const std::string filter_public_libraries(
149 uint32_t target_sdk_version, const std::vector<std::string>& uses_libraries,
150 const std::string& public_libraries) {
151 // Apps targeting Android 11 or earlier gets all public libraries
152 if (target_sdk_version <= 30) {
153 return public_libraries;
154 }
155 if (std::find(uses_libraries.begin(), uses_libraries.end(), LIBRARY_ALL) !=
156 uses_libraries.end()) {
157 return public_libraries;
158 }
159 std::vector<std::string> filtered;
160 std::vector<std::string> orig = android::base::Split(public_libraries, ":");
161 for (const auto& lib : uses_libraries) {
162 if (std::find(orig.begin(), orig.end(), lib) != orig.end()) {
163 filtered.emplace_back(lib);
164 }
165 }
166 return android::base::Join(filtered, ":");
167}
168
Orion Hodson9b16e342019-10-09 13:29:16 +0100169Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
170 jobject class_loader, bool is_shared,
Jooyung Han538f99a2020-03-03 00:46:50 +0900171 jstring dex_path_j,
Orion Hodson9b16e342019-10-09 13:29:16 +0100172 jstring java_library_path,
Jiyong Park14626a72020-07-02 23:17:58 +0900173 jstring java_permitted_path,
174 jstring uses_library_list) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100175 std::string library_path; // empty string by default.
Jooyung Han538f99a2020-03-03 00:46:50 +0900176 std::string dex_path;
Orion Hodson9b16e342019-10-09 13:29:16 +0100177
178 if (java_library_path != nullptr) {
179 ScopedUtfChars library_path_utf_chars(env, java_library_path);
180 library_path = library_path_utf_chars.c_str();
181 }
182
Jooyung Han538f99a2020-03-03 00:46:50 +0900183 if (dex_path_j != nullptr) {
184 ScopedUtfChars dex_path_chars(env, dex_path_j);
185 dex_path = dex_path_chars.c_str();
186 }
187
Jiyong Park14626a72020-07-02 23:17:58 +0900188 std::vector<std::string> uses_libraries;
189 if (uses_library_list != nullptr) {
190 ScopedUtfChars names(env, uses_library_list);
191 uses_libraries = android::base::Split(names.c_str(), ":");
192 } else {
193 // uses_library_list could be nullptr when System.loadLibrary is called from a
194 // custom classloader. In that case, we don't know the list of public
195 // libraries because we don't know which apk the classloader is for. Only
196 // choices we can have are 1) allowing all public libs (as before), or 2)
197 // not allowing all but NDK libs. Here we take #1 because #2 would surprise
198 // developers unnecessarily.
199 // TODO(b/142191088) finalize the policy here. We could either 1) allow all
200 // public libs, 2) disallow any lib, or 3) use the libs that were granted to
201 // the first (i.e. app main) classloader.
202 uses_libraries.emplace_back(LIBRARY_ALL);
203 }
204
Jooyung Han538f99a2020-03-03 00:46:50 +0900205 ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path);
Orion Hodson9b16e342019-10-09 13:29:16 +0100206
207 // (http://b/27588281) This is a workaround for apps using custom
208 // classloaders and calling System.load() with an absolute path which
209 // is outside of the classloader library search path.
210 //
211 // This part effectively allows such a classloader to access anything
212 // under /data and /mnt/expand
213 std::string permitted_path = kWhitelistedDirectories;
214
215 if (java_permitted_path != nullptr) {
216 ScopedUtfChars path(env, java_permitted_path);
217 if (path.c_str() != nullptr && path.size() > 0) {
218 permitted_path = permitted_path + ":" + path.c_str();
219 }
220 }
221
222 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
223 "There is already a namespace associated with this classloader");
224
225 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100226 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900227 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100228 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900229 (apk_origin == APK_ORIGIN_PRODUCT &&
230 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100231 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900232 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100233 // For vendor / product apks, give access to the vendor / product lib even though
234 // they are treated as unbundled; the libs and apks are still bundled
235 // together in the vendor / product partition.
236 const char* origin_partition;
237 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900238 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100239
240 switch (apk_origin) {
241 case APK_ORIGIN_VENDOR:
242 origin_partition = "vendor";
243 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900244 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100245 break;
246 case APK_ORIGIN_PRODUCT:
247 origin_partition = "product";
248 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900249 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100250 break;
251 default:
252 origin_partition = "unknown";
253 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900254 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100255 }
256 library_path = library_path + ":" + origin_lib_path;
257 permitted_path = permitted_path + ":" + origin_lib_path;
258
Justin Yun089c1352020-02-06 16:53:08 +0900259 // Also give access to LLNDK libraries since they are available to vendor or product
260 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100261
262 // Different name is useful for debugging
263 namespace_name = kVendorClassloaderNamespaceName;
264 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
265 origin_partition, library_path.c_str());
266 } else {
Jiyong Park14626a72020-07-02 23:17:58 +0900267 auto libs = filter_public_libraries(target_sdk_version, uses_libraries,
268 extended_public_libraries());
Orion Hodson9b16e342019-10-09 13:29:16 +0100269 // extended public libraries are NOT available to vendor apks, otherwise it
270 // would be system->vendor violation.
Jiyong Park14626a72020-07-02 23:17:58 +0900271 if (!libs.empty()) {
272 system_exposed_libraries = system_exposed_libraries + ':' + libs;
Orion Hodson9b16e342019-10-09 13:29:16 +0100273 }
274 }
275
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100276 if (is_shared) {
277 // Show in the name that the namespace was created as shared, for debugging
278 // purposes.
279 namespace_name = namespace_name + kSharedNamespaceSuffix;
280 }
281
Orion Hodson9b16e342019-10-09 13:29:16 +0100282 // Create the app namespace
283 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
284 // Heuristic: the first classloader with non-empty library_path is assumed to
285 // be the main classloader for app
286 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
287 // friends) and then passing it down to here.
288 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
289 // Policy: the namespace for the main classloader is also used as the
290 // anonymous namespace.
291 bool also_used_as_anonymous = is_main_classloader;
292 // Note: this function is executed with g_namespaces_mutex held, thus no
293 // racing here.
294 auto app_ns = NativeLoaderNamespace::Create(
295 namespace_name, library_path, permitted_path, parent_ns, is_shared,
Ryan Prichard232db5e2020-08-03 15:32:13 -0700296 target_sdk_version < 24 /* is_exempt_list_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900297 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100298 return app_ns.error();
299 }
300 // ... and link to other namespaces to allow access to some public libraries
301 bool is_bridged = app_ns->IsBridged();
302
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000303 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
304 if (!system_ns.ok()) {
305 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100306 }
307
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000308 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900309 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100310 return linked.error();
311 }
312
Jooyung Hancd616d02020-09-01 14:53:23 +0900313 for (const auto&[apex_ns_name, public_libs] : apex_public_libraries()) {
314 auto ns = NativeLoaderNamespace::GetExportedNamespace(apex_ns_name, is_bridged);
315 // Even if APEX namespace is visible, it may not be available to bridged.
316 if (ns.ok()) {
317 linked = app_ns->Link(*ns, public_libs);
318 if (!linked.ok()) {
319 return linked.error();
320 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100321 }
322 }
323
Justin Yuneb4f08c2020-02-18 11:29:07 +0900324 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
325 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100326 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900327 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900328 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
329 if (!linked.ok()) {
330 return linked.error();
331 }
332 }
333 }
334
335 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
336 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
337 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
338 if (vndk_ns.ok()) {
339 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900340 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100341 return linked.error();
342 }
343 }
344 }
345
Jooyung Han538f99a2020-03-03 00:46:50 +0900346 auto apex_ns_name = FindApexNamespaceName(dex_path);
347 if (apex_ns_name.ok()) {
348 const auto& jni_libs = apex_jni_libraries(*apex_ns_name);
349 if (jni_libs != "") {
350 auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
351 if (apex_ns.ok()) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900352 linked = app_ns->Link(*apex_ns, jni_libs);
353 if (!linked.ok()) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900354 return linked.error();
355 }
356 }
357 }
358 }
359
Jiyong Park14626a72020-07-02 23:17:58 +0900360 auto vendor_libs = filter_public_libraries(target_sdk_version, uses_libraries,
361 vendor_public_libraries());
362 if (!vendor_libs.empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100363 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900364 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000365 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900366 if (target_ns.ok()) {
Jiyong Park14626a72020-07-02 23:17:58 +0900367 linked = app_ns->Link(*target_ns, vendor_libs);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900368 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100369 return linked.error();
370 }
371 }
372 }
373
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000374 auto& emplaced = namespaces_.emplace_back(
375 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100376 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000377 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100378 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000379 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100380}
381
382NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
383 jobject class_loader) {
384 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
385 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
386 return env->IsSameObject(value.first, class_loader);
387 });
388 if (it != namespaces_.end()) {
389 return &it->second;
390 }
391
392 return nullptr;
393}
394
395NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
396 jobject class_loader) {
397 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
398
399 while (parent_class_loader != nullptr) {
400 NativeLoaderNamespace* ns;
401 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
402 return ns;
403 }
404
405 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
406 }
407
408 return nullptr;
409}
410
Jooyung Han538f99a2020-03-03 00:46:50 +0900411base::Result<std::string> FindApexNamespaceName(const std::string& location) {
412 // Lots of implicit assumptions here: we expect `location` to be of the form:
413 // /apex/modulename/...
414 //
415 // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it.
416 if (android::base::StartsWith(location, kApexPath)) {
417 size_t start_index = strlen(kApexPath);
418 size_t slash_index = location.find_first_of('/', start_index);
419 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
420 "Error finding namespace of apex: no slash in path %s", location.c_str());
421 std::string name = location.substr(start_index, slash_index - start_index);
422 std::replace(name.begin(), name.end(), '.', '_');
423 return name;
424 }
425 return base::Error();
426}
427
Orion Hodson9b16e342019-10-09 13:29:16 +0100428} // namespace android::nativeloader
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100429
430#endif // defined(ART_TARGET_ANDROID)