blob: 99587e4142b9dc1c1de73c03f190b8985853081f [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";
45constexpr const char* kArtNamespaceName = "art";
46constexpr const char* kNeuralNetworksNamespaceName = "neuralnetworks";
Luke Huang5c017722019-12-17 10:54:26 +080047constexpr const char* kCronetNamespaceName = "cronet";
Orion Hodson9b16e342019-10-09 13:29:16 +010048
49// classloader-namespace is a linker namespace that is created for the loaded
50// app. To be specific, it is created for the app classloader. When
51// System.load() is called from a Java class that is loaded from the
52// classloader, the classloader-namespace namespace associated with that
53// classloader is selected for dlopen. The namespace is configured so that its
54// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090055// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010056// This way an app can only load its own JNI libraries along with the public libs.
57constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
58// Same thing for vendor APKs.
59constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010060// If the namespace is shared then add this suffix to form
61// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
62// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
63// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090064// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010065// access to the platform libraries for apps bundled in the system image,
66// including their later updates installed in /data.
67constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010068
69// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
70// System.load() with an absolute path which is outside of the classloader library search path.
71// This list includes all directories app is allowed to access this way.
72constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
73
74constexpr const char* kVendorLibPath = "/vendor/" LIB;
75constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
76
77const std::regex kVendorDexPathRegex("(^|:)/vendor/");
78const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
79
80// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010081using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010082 APK_ORIGIN_DEFAULT = 0,
83 APK_ORIGIN_VENDOR = 1,
84 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010085};
Orion Hodson9b16e342019-10-09 13:29:16 +010086
87jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
88 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
89 jmethodID get_parent =
90 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
91
92 return env->CallObjectMethod(class_loader, get_parent);
93}
94
95ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
96 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
97
98 if (dex_path != nullptr) {
99 ScopedUtfChars dex_path_utf_chars(env, dex_path);
100
101 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
102 apk_origin = APK_ORIGIN_VENDOR;
103 }
104
105 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
106 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
107 "Dex path contains both vendor and product partition : %s",
108 dex_path_utf_chars.c_str());
109
110 apk_origin = APK_ORIGIN_PRODUCT;
111 }
112 }
113 return apk_origin;
114}
115
116} // namespace
117
118void LibraryNamespaces::Initialize() {
119 // Once public namespace is initialized there is no
120 // point in running this code - it will have no effect
121 // on the current list of public libraries.
122 if (initialized_) {
123 return;
124 }
125
126 // android_init_namespaces() expects all the public libraries
127 // to be loaded so that they can be found by soname alone.
128 //
129 // TODO(dimitry): this is a bit misleading since we do not know
130 // if the vendor public library is going to be opened from /vendor/lib
131 // we might as well end up loading them from /system/lib or /product/lib
132 // For now we rely on CTS test to catch things like this but
133 // it should probably be addressed in the future.
134 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
135 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
136 "Error preloading public library %s: %s", soname.c_str(), dlerror());
137 }
138}
139
140Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
141 jobject class_loader, bool is_shared,
142 jstring dex_path,
143 jstring java_library_path,
144 jstring java_permitted_path) {
145 std::string library_path; // empty string by default.
146
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
152 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
153
154 // (http://b/27588281) This is a workaround for apps using custom
155 // classloaders and calling System.load() with an absolute path which
156 // is outside of the classloader library search path.
157 //
158 // This part effectively allows such a classloader to access anything
159 // under /data and /mnt/expand
160 std::string permitted_path = kWhitelistedDirectories;
161
162 if (java_permitted_path != nullptr) {
163 ScopedUtfChars path(env, java_permitted_path);
164 if (path.c_str() != nullptr && path.size() > 0) {
165 permitted_path = permitted_path + ":" + path.c_str();
166 }
167 }
168
169 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
170 "There is already a namespace associated with this classloader");
171
172 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100173 std::string namespace_name = kClassloaderNamespaceName;
Orion Hodson9b16e342019-10-09 13:29:16 +0100174 bool unbundled_vendor_or_product_app = false;
175 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900176 (apk_origin == APK_ORIGIN_PRODUCT &&
177 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100178 !is_shared) {
179 unbundled_vendor_or_product_app = true;
180 // For vendor / product apks, give access to the vendor / product lib even though
181 // they are treated as unbundled; the libs and apks are still bundled
182 // together in the vendor / product partition.
183 const char* origin_partition;
184 const char* origin_lib_path;
185
186 switch (apk_origin) {
187 case APK_ORIGIN_VENDOR:
188 origin_partition = "vendor";
189 origin_lib_path = kVendorLibPath;
190 break;
191 case APK_ORIGIN_PRODUCT:
192 origin_partition = "product";
193 origin_lib_path = kProductLibPath;
194 break;
195 default:
196 origin_partition = "unknown";
197 origin_lib_path = "";
198 }
199 library_path = library_path + ":" + origin_lib_path;
200 permitted_path = permitted_path + ":" + origin_lib_path;
201
202 // Also give access to LLNDK libraries since they are available to vendors
203 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries().c_str();
204
205 // Different name is useful for debugging
206 namespace_name = kVendorClassloaderNamespaceName;
207 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
208 origin_partition, library_path.c_str());
209 } else {
210 // extended public libraries are NOT available to vendor apks, otherwise it
211 // would be system->vendor violation.
212 if (!extended_public_libraries().empty()) {
213 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
214 }
215 }
216
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100217 if (is_shared) {
218 // Show in the name that the namespace was created as shared, for debugging
219 // purposes.
220 namespace_name = namespace_name + kSharedNamespaceSuffix;
221 }
222
Orion Hodson9b16e342019-10-09 13:29:16 +0100223 // Create the app namespace
224 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
225 // Heuristic: the first classloader with non-empty library_path is assumed to
226 // be the main classloader for app
227 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
228 // friends) and then passing it down to here.
229 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
230 // Policy: the namespace for the main classloader is also used as the
231 // anonymous namespace.
232 bool also_used_as_anonymous = is_main_classloader;
233 // Note: this function is executed with g_namespaces_mutex held, thus no
234 // racing here.
235 auto app_ns = NativeLoaderNamespace::Create(
236 namespace_name, library_path, permitted_path, parent_ns, is_shared,
237 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900238 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100239 return app_ns.error();
240 }
241 // ... and link to other namespaces to allow access to some public libraries
242 bool is_bridged = app_ns->IsBridged();
243
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900244 auto platform_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900245 if (!platform_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100246 return platform_ns.error();
247 }
248
249 auto linked = app_ns->Link(*platform_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900250 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100251 return linked.error();
252 }
253
254 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
255 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900256 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100257 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900258 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100259 return linked.error();
260 }
261 }
262
263 // Give access to NNAPI libraries (apex-updated LLNDK library).
264 auto nnapi_ns =
265 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900266 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100267 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900268 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100269 return linked.error();
270 }
271 }
272
273 // Give access to VNDK-SP libraries from the 'vndk' namespace.
274 if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
275 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900276 if (vndk_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100277 linked = app_ns->Link(*vndk_ns, vndksp_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900278 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100279 return linked.error();
280 }
281 }
282 }
283
Luke Huang5c017722019-12-17 10:54:26 +0800284 // TODO(b/143733063): Remove it after library path of apex module is supported.
285 auto cronet_ns =
286 NativeLoaderNamespace::GetExportedNamespace(kCronetNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900287 if (cronet_ns.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800288 linked = app_ns->Link(*cronet_ns, cronet_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900289 if (!linked.ok()) {
Luke Huang5c017722019-12-17 10:54:26 +0800290 return linked.error();
291 }
292 }
293
Orion Hodson9b16e342019-10-09 13:29:16 +0100294 if (!vendor_public_libraries().empty()) {
295 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900296 // when vendor_ns is not configured, link to the system namespace
Bernie Innocentib68b7cb2020-02-09 15:50:56 +0900297 auto target_ns = vendor_ns.ok() ? vendor_ns : platform_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900298 if (target_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100299 linked = app_ns->Link(*target_ns, vendor_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900300 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100301 return linked.error();
302 }
303 }
304 }
305
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000306 auto& emplaced = namespaces_.emplace_back(
307 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100308 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000309 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100310 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000311 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100312}
313
314NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
315 jobject class_loader) {
316 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
317 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
318 return env->IsSameObject(value.first, class_loader);
319 });
320 if (it != namespaces_.end()) {
321 return &it->second;
322 }
323
324 return nullptr;
325}
326
327NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
328 jobject class_loader) {
329 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
330
331 while (parent_class_loader != nullptr) {
332 NativeLoaderNamespace* ns;
333 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
334 return ns;
335 }
336
337 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
338 }
339
340 return nullptr;
341}
342
343} // namespace android::nativeloader