blob: b80e9a1b64a2d1b4174033ae44c55b6511813b0e [file] [log] [blame]
Jiyong Park6291da22019-04-26 18:55:48 +09001/*
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"
30#include "nativehelper/ScopedUtfChars.h"
31#include "nativeloader/dlext_namespaces.h"
Jiyong Park40a60772019-05-03 16:21:31 +090032#include "public_libraries.h"
Jiyong Park6291da22019-04-26 18:55:48 +090033
Jiyong Park40a60772019-05-03 16:21:31 +090034namespace android::nativeloader {
Jiyong Park6291da22019-04-26 18:55:48 +090035
36namespace {
Jiyong Park6291da22019-04-26 18:55:48 +090037// The device may be configured to have the vendor libraries loaded to a separate namespace.
38// For historical reasons this namespace was named sphal but effectively it is intended
39// to use to load vendor libraries to separate namespace with controlled interface between
40// vendor and system namespaces.
41constexpr const char* kVendorNamespaceName = "sphal";
42constexpr const char* kVndkNamespaceName = "vndk";
43constexpr const char* kDefaultNamespaceName = "default";
44constexpr const char* kPlatformNamespaceName = "platform";
45constexpr const char* kRuntimeNamespaceName = "runtime";
46
47// classloader-namespace is a linker namespace that is created for the loaded
48// app. To be specific, it is created for the app classloader. When
49// System.load() is called from a Java class that is loaded from the
50// classloader, the classloader-namespace namespace associated with that
51// classloader is selected for dlopen. The namespace is configured so that its
52// search path is set to the app-local JNI directory and it is linked to the
53// platform namespace with the names of libs listed in the public.libraries.txt.
54// This way an app can only load its own JNI libraries along with the public libs.
55constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
56// Same thing for vendor APKs.
57constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
58
59// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
60// System.load() with an absolute path which is outside of the classloader library search path.
61// This list includes all directories app is allowed to access this way.
62constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
63
Jiyong Park40a60772019-05-03 16:21:31 +090064// TODO(b/130388701) use macro LIB to eliminate the conditional
Jiyong Park6291da22019-04-26 18:55:48 +090065#if defined(__LP64__)
Jiyong Park6291da22019-04-26 18:55:48 +090066constexpr const char* kVendorLibPath = "/vendor/lib64";
67constexpr const char* kProductLibPath = "/product/lib64:/system/product/lib64";
68#else
Jiyong Park6291da22019-04-26 18:55:48 +090069constexpr const char* kVendorLibPath = "/vendor/lib";
70constexpr const char* kProductLibPath = "/product/lib:/system/product/lib";
71#endif
72
73const std::regex kVendorDexPathRegex("(^|:)/vendor/");
74const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
75
76// Define origin of APK if it is from vendor partition or product partition
77typedef enum {
78 APK_ORIGIN_DEFAULT = 0,
79 APK_ORIGIN_VENDOR = 1,
80 APK_ORIGIN_PRODUCT = 2,
81} ApkOrigin;
82
Jiyong Park6291da22019-04-26 18:55:48 +090083jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
84 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
85 jmethodID get_parent =
86 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
87
88 return env->CallObjectMethod(class_loader, get_parent);
89}
90
91ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
92 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
93
94 if (dex_path != nullptr) {
95 ScopedUtfChars dex_path_utf_chars(env, dex_path);
96
97 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
98 apk_origin = APK_ORIGIN_VENDOR;
99 }
100
101 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
102 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
103 "Dex path contains both vendor and product partition : %s",
104 dex_path_utf_chars.c_str());
105
106 apk_origin = APK_ORIGIN_PRODUCT;
107 }
108 }
109 return apk_origin;
110}
111
112} // namespace
113
114void LibraryNamespaces::Initialize() {
115 // Once public namespace is initialized there is no
116 // point in running this code - it will have no effect
117 // on the current list of public libraries.
118 if (initialized_) {
119 return;
120 }
121
Jiyong Park6291da22019-04-26 18:55:48 +0900122 // android_init_namespaces() expects all the public libraries
123 // to be loaded so that they can be found by soname alone.
124 //
125 // TODO(dimitry): this is a bit misleading since we do not know
126 // if the vendor public library is going to be opened from /vendor/lib
127 // we might as well end up loading them from /system/lib or /product/lib
128 // For now we rely on CTS test to catch things like this but
129 // it should probably be addressed in the future.
Jiyong Park40a60772019-05-03 16:21:31 +0900130 for (const auto& soname : android::base::Split(system_public_libraries(), ":")) {
Jiyong Park6291da22019-04-26 18:55:48 +0900131 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
132 "Error preloading public library %s: %s", soname.c_str(), dlerror());
133 }
Jiyong Park6291da22019-04-26 18:55:48 +0900134}
135
136NativeLoaderNamespace* LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
137 jobject class_loader, bool is_shared,
138 jstring dex_path, jstring java_library_path,
139 jstring java_permitted_path,
140 std::string* error_msg) {
141 std::string library_path; // empty string by default.
142
143 if (java_library_path != nullptr) {
144 ScopedUtfChars library_path_utf_chars(env, java_library_path);
145 library_path = library_path_utf_chars.c_str();
146 }
147
148 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
149
150 // (http://b/27588281) This is a workaround for apps using custom
151 // classloaders and calling System.load() with an absolute path which
152 // is outside of the classloader library search path.
153 //
154 // This part effectively allows such a classloader to access anything
155 // under /data and /mnt/expand
156 std::string permitted_path = kWhitelistedDirectories;
157
158 if (java_permitted_path != nullptr) {
159 ScopedUtfChars path(env, java_permitted_path);
160 if (path.c_str() != nullptr && path.size() > 0) {
161 permitted_path = permitted_path + ":" + path.c_str();
162 }
163 }
164
165 // Initialize the anonymous namespace with the first non-empty library path.
166 if (!library_path.empty() && !initialized_ &&
167 !InitPublicNamespace(library_path.c_str(), error_msg)) {
168 return nullptr;
169 }
170
171 bool found = FindNamespaceByClassLoader(env, class_loader);
172
173 LOG_ALWAYS_FATAL_IF(found, "There is already a namespace associated with this classloader");
174
175 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
176 if (is_shared) {
177 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
178 }
179
180 if (target_sdk_version < 24) {
181 namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
182 }
183
184 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
185
186 bool is_native_bridge = false;
187
188 if (parent_ns != nullptr) {
189 is_native_bridge = !parent_ns->is_android_namespace();
190 } else if (!library_path.empty()) {
191 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
192 }
193
Jiyong Park40a60772019-05-03 16:21:31 +0900194 std::string system_exposed_libraries = system_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900195 const char* namespace_name = kClassloaderNamespaceName;
196 android_namespace_t* vndk_ns = nullptr;
197 if ((apk_origin == APK_ORIGIN_VENDOR ||
198 (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) &&
199 !is_shared) {
200 LOG_FATAL_IF(is_native_bridge,
201 "Unbundled vendor / product apk must not use translated architecture");
202
203 // For vendor / product apks, give access to the vendor / product lib even though
204 // they are treated as unbundled; the libs and apks are still bundled
205 // together in the vendor / product partition.
206 const char* origin_partition;
207 const char* origin_lib_path;
208
209 switch (apk_origin) {
210 case APK_ORIGIN_VENDOR:
211 origin_partition = "vendor";
212 origin_lib_path = kVendorLibPath;
213 break;
214 case APK_ORIGIN_PRODUCT:
215 origin_partition = "product";
216 origin_lib_path = kProductLibPath;
217 break;
218 default:
219 origin_partition = "unknown";
220 origin_lib_path = "";
221 }
222
223 LOG_FATAL_IF(is_native_bridge, "Unbundled %s apk must not use translated architecture",
224 origin_partition);
225
226 library_path = library_path + ":" + origin_lib_path;
227 permitted_path = permitted_path + ":" + origin_lib_path;
228
229 // Also give access to LLNDK libraries since they are available to vendors
Jiyong Park40a60772019-05-03 16:21:31 +0900230 system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries().c_str();
Jiyong Park6291da22019-04-26 18:55:48 +0900231
232 // Give access to VNDK-SP libraries from the 'vndk' namespace.
233 vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
234 if (vndk_ns == nullptr) {
235 ALOGW("Cannot find \"%s\" namespace for %s apks", kVndkNamespaceName, origin_partition);
236 }
237
238 // Different name is useful for debugging
239 namespace_name = kVendorClassloaderNamespaceName;
240 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
241 origin_partition, library_path.c_str());
242 } else {
243 // oem and product public libraries are NOT available to vendor apks, otherwise it
244 // would be system->vendor violation.
Jiyong Park40a60772019-05-03 16:21:31 +0900245 if (!oem_public_libraries().empty()) {
246 system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900247 }
Jiyong Park40a60772019-05-03 16:21:31 +0900248 if (!product_public_libraries().empty()) {
249 system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900250 }
251 }
Jiyong Park40a60772019-05-03 16:21:31 +0900252 std::string runtime_exposed_libraries = runtime_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900253
254 NativeLoaderNamespace native_loader_ns;
255 if (!is_native_bridge) {
256 // The platform namespace is called "default" for binaries in /system and
257 // "platform" for those in the Runtime APEX. Try "platform" first since
258 // "default" always exists.
259 android_namespace_t* platform_ns = android_get_exported_namespace(kPlatformNamespaceName);
260 if (platform_ns == nullptr) {
261 platform_ns = android_get_exported_namespace(kDefaultNamespaceName);
262 }
263
264 android_namespace_t* android_parent_ns;
265 if (parent_ns != nullptr) {
266 android_parent_ns = parent_ns->get_android_ns();
267 } else {
268 // Fall back to the platform namespace if no parent is found.
269 android_parent_ns = platform_ns;
270 }
271
272 android_namespace_t* ns =
273 android_create_namespace(namespace_name, nullptr, library_path.c_str(), namespace_type,
274 permitted_path.c_str(), android_parent_ns);
275 if (ns == nullptr) {
276 *error_msg = dlerror();
277 return nullptr;
278 }
279
280 // Note that when vendor_ns is not configured this function will return nullptr
281 // and it will result in linking vendor_public_libraries_ to the default namespace
282 // which is expected behavior in this case.
283 android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
284
285 android_namespace_t* runtime_ns = android_get_exported_namespace(kRuntimeNamespaceName);
286
287 if (!android_link_namespaces(ns, platform_ns, system_exposed_libraries.c_str())) {
288 *error_msg = dlerror();
289 return nullptr;
290 }
291
292 // Runtime apex does not exist in host, and under certain build conditions.
293 if (runtime_ns != nullptr) {
294 if (!android_link_namespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) {
295 *error_msg = dlerror();
296 return nullptr;
297 }
298 }
299
Jiyong Park40a60772019-05-03 16:21:31 +0900300 if (vndk_ns != nullptr && !system_vndksp_libraries().empty()) {
Jiyong Park6291da22019-04-26 18:55:48 +0900301 // vendor apks are allowed to use VNDK-SP libraries.
Jiyong Park40a60772019-05-03 16:21:31 +0900302 if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries().c_str())) {
Jiyong Park6291da22019-04-26 18:55:48 +0900303 *error_msg = dlerror();
304 return nullptr;
305 }
306 }
307
Jiyong Park40a60772019-05-03 16:21:31 +0900308 if (!vendor_public_libraries().empty()) {
309 if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries().c_str())) {
Jiyong Park6291da22019-04-26 18:55:48 +0900310 *error_msg = dlerror();
311 return nullptr;
312 }
313 }
314
315 native_loader_ns = NativeLoaderNamespace(ns);
316 } else {
317 // Same functionality as in the branch above, but calling through native bridge.
318
319 native_bridge_namespace_t* platform_ns =
320 NativeBridgeGetExportedNamespace(kPlatformNamespaceName);
321 if (platform_ns == nullptr) {
322 platform_ns = NativeBridgeGetExportedNamespace(kDefaultNamespaceName);
323 }
324
325 native_bridge_namespace_t* native_bridge_parent_namespace;
326 if (parent_ns != nullptr) {
327 native_bridge_parent_namespace = parent_ns->get_native_bridge_ns();
328 } else {
329 native_bridge_parent_namespace = platform_ns;
330 }
331
332 native_bridge_namespace_t* ns =
333 NativeBridgeCreateNamespace(namespace_name, nullptr, library_path.c_str(), namespace_type,
334 permitted_path.c_str(), native_bridge_parent_namespace);
335 if (ns == nullptr) {
336 *error_msg = NativeBridgeGetError();
337 return nullptr;
338 }
339
340 native_bridge_namespace_t* vendor_ns = NativeBridgeGetExportedNamespace(kVendorNamespaceName);
341 native_bridge_namespace_t* runtime_ns = NativeBridgeGetExportedNamespace(kRuntimeNamespaceName);
342
343 if (!NativeBridgeLinkNamespaces(ns, platform_ns, system_exposed_libraries.c_str())) {
344 *error_msg = NativeBridgeGetError();
345 return nullptr;
346 }
347
348 // Runtime apex does not exist in host, and under certain build conditions.
349 if (runtime_ns != nullptr) {
350 if (!NativeBridgeLinkNamespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) {
351 *error_msg = NativeBridgeGetError();
352 return nullptr;
353 }
354 }
Jiyong Park40a60772019-05-03 16:21:31 +0900355 if (!vendor_public_libraries().empty()) {
356 if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries().c_str())) {
Jiyong Park6291da22019-04-26 18:55:48 +0900357 *error_msg = NativeBridgeGetError();
358 return nullptr;
359 }
360 }
361
362 native_loader_ns = NativeLoaderNamespace(ns);
363 }
364
365 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
366
367 return &(namespaces_.back().second);
368}
369
370NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
371 jobject class_loader) {
372 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
373 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
374 return env->IsSameObject(value.first, class_loader);
375 });
376 if (it != namespaces_.end()) {
377 return &it->second;
378 }
379
380 return nullptr;
381}
382
383bool LibraryNamespaces::InitPublicNamespace(const char* library_path, std::string* error_msg) {
384 // Ask native bride if this apps library path should be handled by it
385 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
386
387 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
388 // code is one example) unknown to linker in which case linker uses anonymous
389 // namespace. The second argument specifies the search path for the anonymous
390 // namespace which is the library_path of the classloader.
Jiyong Park40a60772019-05-03 16:21:31 +0900391 initialized_ = android_init_anonymous_namespace(system_public_libraries().c_str(),
Jiyong Park6291da22019-04-26 18:55:48 +0900392 is_native_bridge ? nullptr : library_path);
393 if (!initialized_) {
394 *error_msg = dlerror();
395 return false;
396 }
397
398 // and now initialize native bridge namespaces if necessary.
399 if (NativeBridgeInitialized()) {
Jiyong Park40a60772019-05-03 16:21:31 +0900400 initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries().c_str(),
Jiyong Park6291da22019-04-26 18:55:48 +0900401 is_native_bridge ? library_path : nullptr);
402 if (!initialized_) {
403 *error_msg = NativeBridgeGetError();
404 }
405 }
406
407 return initialized_;
408}
409
410NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
411 jobject class_loader) {
412 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
413
414 while (parent_class_loader != nullptr) {
415 NativeLoaderNamespace* ns;
416 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
417 return ns;
418 }
419
420 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
421 }
422
423 return nullptr;
424}
425
Jiyong Park40a60772019-05-03 16:21:31 +0900426} // namespace android::nativeloader