blob: 6092c004efe0ab0282dea557e46a5cd328f74e34 [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>
Jooyung Han538f99a2020-03-03 00:46:50 +090029#include <android-base/result.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010030#include <android-base/strings.h>
Orion Hodson6dc0a432020-02-06 14:28:28 +000031#include <nativehelper/scoped_utf_chars.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010032
33#include "nativeloader/dlext_namespaces.h"
34#include "public_libraries.h"
35#include "utils.h"
36
Orion Hodson9b16e342019-10-09 13:29:16 +010037namespace android::nativeloader {
38
39namespace {
Jooyung Han538f99a2020-03-03 00:46:50 +090040
41constexpr const char* kApexPath = "/apex/";
42
Orion Hodson9b16e342019-10-09 13:29:16 +010043// The device may be configured to have the vendor libraries loaded to a separate namespace.
44// For historical reasons this namespace was named sphal but effectively it is intended
45// to use to load vendor libraries to separate namespace with controlled interface between
46// vendor and system namespaces.
47constexpr const char* kVendorNamespaceName = "sphal";
48constexpr const char* kVndkNamespaceName = "vndk";
Justin Yuneb4f08c2020-02-18 11:29:07 +090049constexpr const char* kVndkProductNamespaceName = "vndk_product";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090050constexpr const char* kArtNamespaceName = "com_android_art";
51constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090052constexpr const char* kStatsdNamespaceName = "com_android_os_statsd";
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
139Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
140 jobject class_loader, bool is_shared,
Jooyung Han538f99a2020-03-03 00:46:50 +0900141 jstring dex_path_j,
Orion Hodson9b16e342019-10-09 13:29:16 +0100142 jstring java_library_path,
143 jstring java_permitted_path) {
144 std::string library_path; // empty string by default.
Jooyung Han538f99a2020-03-03 00:46:50 +0900145 std::string dex_path;
Orion Hodson9b16e342019-10-09 13:29:16 +0100146
147 if (java_library_path != nullptr) {
148 ScopedUtfChars library_path_utf_chars(env, java_library_path);
149 library_path = library_path_utf_chars.c_str();
150 }
151
Jooyung Han538f99a2020-03-03 00:46:50 +0900152 if (dex_path_j != nullptr) {
153 ScopedUtfChars dex_path_chars(env, dex_path_j);
154 dex_path = dex_path_chars.c_str();
155 }
156
157 ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path);
Orion Hodson9b16e342019-10-09 13:29:16 +0100158
159 // (http://b/27588281) This is a workaround for apps using custom
160 // classloaders and calling System.load() with an absolute path which
161 // is outside of the classloader library search path.
162 //
163 // This part effectively allows such a classloader to access anything
164 // under /data and /mnt/expand
165 std::string permitted_path = kWhitelistedDirectories;
166
167 if (java_permitted_path != nullptr) {
168 ScopedUtfChars path(env, java_permitted_path);
169 if (path.c_str() != nullptr && path.size() > 0) {
170 permitted_path = permitted_path + ":" + path.c_str();
171 }
172 }
173
174 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
175 "There is already a namespace associated with this classloader");
176
177 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100178 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900179 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100180 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900181 (apk_origin == APK_ORIGIN_PRODUCT &&
182 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100183 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900184 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100185 // For vendor / product apks, give access to the vendor / product lib even though
186 // they are treated as unbundled; the libs and apks are still bundled
187 // together in the vendor / product partition.
188 const char* origin_partition;
189 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900190 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100191
192 switch (apk_origin) {
193 case APK_ORIGIN_VENDOR:
194 origin_partition = "vendor";
195 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900196 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100197 break;
198 case APK_ORIGIN_PRODUCT:
199 origin_partition = "product";
200 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900201 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100202 break;
203 default:
204 origin_partition = "unknown";
205 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900206 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100207 }
208 library_path = library_path + ":" + origin_lib_path;
209 permitted_path = permitted_path + ":" + origin_lib_path;
210
Justin Yun089c1352020-02-06 16:53:08 +0900211 // Also give access to LLNDK libraries since they are available to vendor or product
212 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100213
214 // Different name is useful for debugging
215 namespace_name = kVendorClassloaderNamespaceName;
216 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
217 origin_partition, library_path.c_str());
218 } else {
219 // extended public libraries are NOT available to vendor apks, otherwise it
220 // would be system->vendor violation.
221 if (!extended_public_libraries().empty()) {
222 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
223 }
224 }
225
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100226 if (is_shared) {
227 // Show in the name that the namespace was created as shared, for debugging
228 // purposes.
229 namespace_name = namespace_name + kSharedNamespaceSuffix;
230 }
231
Orion Hodson9b16e342019-10-09 13:29:16 +0100232 // Create the app namespace
233 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
234 // Heuristic: the first classloader with non-empty library_path is assumed to
235 // be the main classloader for app
236 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
237 // friends) and then passing it down to here.
238 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
239 // Policy: the namespace for the main classloader is also used as the
240 // anonymous namespace.
241 bool also_used_as_anonymous = is_main_classloader;
242 // Note: this function is executed with g_namespaces_mutex held, thus no
243 // racing here.
244 auto app_ns = NativeLoaderNamespace::Create(
245 namespace_name, library_path, permitted_path, parent_ns, is_shared,
246 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900247 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100248 return app_ns.error();
249 }
250 // ... and link to other namespaces to allow access to some public libraries
251 bool is_bridged = app_ns->IsBridged();
252
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000253 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
254 if (!system_ns.ok()) {
255 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100256 }
257
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000258 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900259 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100260 return linked.error();
261 }
262
263 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
264 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900265 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100266 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900267 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100268 return linked.error();
269 }
270 }
271
272 // Give access to NNAPI libraries (apex-updated LLNDK library).
273 auto nnapi_ns =
274 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900275 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100276 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900277 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100278 return linked.error();
279 }
280 }
281
Justin Yuneb4f08c2020-02-18 11:29:07 +0900282 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
283 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100284 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900285 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900286 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
287 if (!linked.ok()) {
288 return linked.error();
289 }
290 }
291 }
292
293 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
294 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
295 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
296 if (vndk_ns.ok()) {
297 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900298 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100299 return linked.error();
300 }
301 }
302 }
303
Jooyung Han538f99a2020-03-03 00:46:50 +0900304 auto apex_ns_name = FindApexNamespaceName(dex_path);
305 if (apex_ns_name.ok()) {
306 const auto& jni_libs = apex_jni_libraries(*apex_ns_name);
307 if (jni_libs != "") {
308 auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
309 if (apex_ns.ok()) {
310 auto link = app_ns->Link(*apex_ns, jni_libs);
311 if (!link.ok()) {
312 return linked.error();
313 }
314 }
315 }
316 }
317
Jeffrey Huang52575032020-02-11 17:33:45 -0800318 // Give access to StatsdAPI libraries
319 auto statsd_ns =
320 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
321 if (statsd_ns.ok()) {
322 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
323 if (!linked.ok()) {
324 return linked.error();
325 }
326 }
327
Orion Hodson9b16e342019-10-09 13:29:16 +0100328 if (!vendor_public_libraries().empty()) {
329 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900330 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000331 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900332 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100333 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900334 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100335 return linked.error();
336 }
337 }
338 }
339
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000340 auto& emplaced = namespaces_.emplace_back(
341 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100342 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000343 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100344 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000345 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100346}
347
348NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
349 jobject class_loader) {
350 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
351 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
352 return env->IsSameObject(value.first, class_loader);
353 });
354 if (it != namespaces_.end()) {
355 return &it->second;
356 }
357
358 return nullptr;
359}
360
361NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
362 jobject class_loader) {
363 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
364
365 while (parent_class_loader != nullptr) {
366 NativeLoaderNamespace* ns;
367 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
368 return ns;
369 }
370
371 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
372 }
373
374 return nullptr;
375}
376
Jooyung Han538f99a2020-03-03 00:46:50 +0900377base::Result<std::string> FindApexNamespaceName(const std::string& location) {
378 // Lots of implicit assumptions here: we expect `location` to be of the form:
379 // /apex/modulename/...
380 //
381 // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it.
382 if (android::base::StartsWith(location, kApexPath)) {
383 size_t start_index = strlen(kApexPath);
384 size_t slash_index = location.find_first_of('/', start_index);
385 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
386 "Error finding namespace of apex: no slash in path %s", location.c_str());
387 std::string name = location.substr(start_index, slash_index - start_index);
388 std::replace(name.begin(), name.end(), '.', '_');
389 return name;
390 }
391 return base::Error();
392}
393
Orion Hodson9b16e342019-10-09 13:29:16 +0100394} // namespace android::nativeloader