blob: 9a33b5576201ab6e7ffe6670904eb140b83e7a54 [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
Jiyong Park16a98962019-05-04 03:10:48 +090025#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
Jiyong Park6291da22019-04-26 18:55:48 +090032#include "nativeloader/dlext_namespaces.h"
Jiyong Park40a60772019-05-03 16:21:31 +090033#include "public_libraries.h"
Jiyong Parkf8802e52019-05-03 16:34:56 +090034#include "utils.h"
Jiyong Park6291da22019-04-26 18:55:48 +090035
Jiyong Park8f4afc82019-07-22 14:20:40 +090036using android::base::Error;
37
Jiyong Park40a60772019-05-03 16:21:31 +090038namespace android::nativeloader {
Jiyong Park6291da22019-04-26 18:55:48 +090039
40namespace {
Jiyong Park6291da22019-04-26 18:55:48 +090041// The device may be configured to have the vendor libraries loaded to a separate namespace.
42// For historical reasons this namespace was named sphal but effectively it is intended
43// to use to load vendor libraries to separate namespace with controlled interface between
44// vendor and system namespaces.
45constexpr const char* kVendorNamespaceName = "sphal";
46constexpr const char* kVndkNamespaceName = "vndk";
Jiyong Park6291da22019-04-26 18:55:48 +090047constexpr const char* kRuntimeNamespaceName = "runtime";
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +010048constexpr const char* kNeuralNetworksNamespaceName = "neuralnetworks";
Jiyong Park6291da22019-04-26 18:55:48 +090049
50// classloader-namespace is a linker namespace that is created for the loaded
51// app. To be specific, it is created for the app classloader. When
52// System.load() is called from a Java class that is loaded from the
53// classloader, the classloader-namespace namespace associated with that
54// classloader is selected for dlopen. The namespace is configured so that its
55// search path is set to the app-local JNI directory and it is linked to the
56// platform namespace with the names of libs listed in the public.libraries.txt.
57// This way an app can only load its own JNI libraries along with the public libs.
58constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
59// Same thing for vendor APKs.
60constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
61
62// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
63// System.load() with an absolute path which is outside of the classloader library search path.
64// This list includes all directories app is allowed to access this way.
65constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
66
Jiyong Parkf8802e52019-05-03 16:34:56 +090067constexpr const char* kVendorLibPath = "/vendor/" LIB;
68constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
Jiyong Park6291da22019-04-26 18:55:48 +090069
70const std::regex kVendorDexPathRegex("(^|:)/vendor/");
71const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
72
73// Define origin of APK if it is from vendor partition or product partition
74typedef enum {
75 APK_ORIGIN_DEFAULT = 0,
76 APK_ORIGIN_VENDOR = 1,
77 APK_ORIGIN_PRODUCT = 2,
78} ApkOrigin;
79
Jiyong Park6291da22019-04-26 18:55:48 +090080jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
81 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
82 jmethodID get_parent =
83 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
84
85 return env->CallObjectMethod(class_loader, get_parent);
86}
87
88ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) {
89 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
90
91 if (dex_path != nullptr) {
92 ScopedUtfChars dex_path_utf_chars(env, dex_path);
93
94 if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) {
95 apk_origin = APK_ORIGIN_VENDOR;
96 }
97
98 if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) {
99 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
100 "Dex path contains both vendor and product partition : %s",
101 dex_path_utf_chars.c_str());
102
103 apk_origin = APK_ORIGIN_PRODUCT;
104 }
105 }
106 return apk_origin;
107}
108
109} // namespace
110
111void LibraryNamespaces::Initialize() {
112 // Once public namespace is initialized there is no
113 // point in running this code - it will have no effect
114 // on the current list of public libraries.
115 if (initialized_) {
116 return;
117 }
118
Jiyong Park6291da22019-04-26 18:55:48 +0900119 // android_init_namespaces() expects all the public libraries
120 // to be loaded so that they can be found by soname alone.
121 //
122 // TODO(dimitry): this is a bit misleading since we do not know
123 // if the vendor public library is going to be opened from /vendor/lib
124 // we might as well end up loading them from /system/lib or /product/lib
125 // For now we rely on CTS test to catch things like this but
126 // it should probably be addressed in the future.
Jiyong Park5db5d192019-07-22 20:29:08 +0900127 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
Jiyong Park6291da22019-04-26 18:55:48 +0900128 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
129 "Error preloading public library %s: %s", soname.c_str(), dlerror());
130 }
Jiyong Park6291da22019-04-26 18:55:48 +0900131}
132
Jiyong Park8f4afc82019-07-22 14:20:40 +0900133Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
134 jobject class_loader, bool is_shared,
135 jstring dex_path,
136 jstring java_library_path,
137 jstring java_permitted_path) {
Jiyong Park6291da22019-04-26 18:55:48 +0900138 std::string library_path; // empty string by default.
139
140 if (java_library_path != nullptr) {
141 ScopedUtfChars library_path_utf_chars(env, java_library_path);
142 library_path = library_path_utf_chars.c_str();
143 }
144
145 ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path);
146
147 // (http://b/27588281) This is a workaround for apps using custom
148 // classloaders and calling System.load() with an absolute path which
149 // is outside of the classloader library search path.
150 //
151 // This part effectively allows such a classloader to access anything
152 // under /data and /mnt/expand
153 std::string permitted_path = kWhitelistedDirectories;
154
155 if (java_permitted_path != nullptr) {
156 ScopedUtfChars path(env, java_permitted_path);
157 if (path.c_str() != nullptr && path.size() > 0) {
158 permitted_path = permitted_path + ":" + path.c_str();
159 }
160 }
161
Jiyong Park8f4afc82019-07-22 14:20:40 +0900162 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
163 "There is already a namespace associated with this classloader");
Jiyong Park6291da22019-04-26 18:55:48 +0900164
Jiyong Park5b8b3062019-05-03 18:11:49 +0900165 std::string system_exposed_libraries = default_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900166 const char* namespace_name = kClassloaderNamespaceName;
Jiyong Park85377812019-05-04 00:30:23 +0900167 bool unbundled_vendor_or_product_app = false;
Jiyong Park6291da22019-04-26 18:55:48 +0900168 if ((apk_origin == APK_ORIGIN_VENDOR ||
169 (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) &&
170 !is_shared) {
Jiyong Park85377812019-05-04 00:30:23 +0900171 unbundled_vendor_or_product_app = true;
Jiyong Park6291da22019-04-26 18:55:48 +0900172 // For vendor / product apks, give access to the vendor / product lib even though
173 // they are treated as unbundled; the libs and apks are still bundled
174 // together in the vendor / product partition.
175 const char* origin_partition;
176 const char* origin_lib_path;
177
178 switch (apk_origin) {
179 case APK_ORIGIN_VENDOR:
180 origin_partition = "vendor";
181 origin_lib_path = kVendorLibPath;
182 break;
183 case APK_ORIGIN_PRODUCT:
184 origin_partition = "product";
185 origin_lib_path = kProductLibPath;
186 break;
187 default:
188 origin_partition = "unknown";
189 origin_lib_path = "";
190 }
Jiyong Park6291da22019-04-26 18:55:48 +0900191 library_path = library_path + ":" + origin_lib_path;
192 permitted_path = permitted_path + ":" + origin_lib_path;
193
194 // Also give access to LLNDK libraries since they are available to vendors
Jiyong Park5b8b3062019-05-03 18:11:49 +0900195 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries().c_str();
Jiyong Park6291da22019-04-26 18:55:48 +0900196
Jiyong Park6291da22019-04-26 18:55:48 +0900197 // Different name is useful for debugging
198 namespace_name = kVendorClassloaderNamespaceName;
199 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
200 origin_partition, library_path.c_str());
201 } else {
Jiyong Park5b8b3062019-05-03 18:11:49 +0900202 // extended public libraries are NOT available to vendor apks, otherwise it
Jiyong Park6291da22019-04-26 18:55:48 +0900203 // would be system->vendor violation.
Jiyong Park5b8b3062019-05-03 18:11:49 +0900204 if (!extended_public_libraries().empty()) {
205 system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
Jiyong Park6291da22019-04-26 18:55:48 +0900206 }
207 }
Jiyong Park6291da22019-04-26 18:55:48 +0900208
Jiyong Park85377812019-05-04 00:30:23 +0900209 // Create the app namespace
210 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
Jiyong Parkb37c4812019-05-16 21:03:30 +0900211 // Heuristic: the first classloader with non-empty library_path is assumed to
212 // be the main classloader for app
213 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
214 // friends) and then passing it down to here.
215 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
216 // Policy: the namespace for the main classloader is also used as the
217 // anonymous namespace.
218 bool also_used_as_anonymous = is_main_classloader;
219 // Note: this function is executed with g_namespaces_mutex held, thus no
220 // racing here.
221 auto app_ns = NativeLoaderNamespace::Create(
222 namespace_name, library_path, permitted_path, parent_ns, is_shared,
223 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900224 if (!app_ns) {
225 return app_ns.error();
Jiyong Park6291da22019-04-26 18:55:48 +0900226 }
Jiyong Park85377812019-05-04 00:30:23 +0900227 // ... and link to other namespaces to allow access to some public libraries
Jiyong Park8f4afc82019-07-22 14:20:40 +0900228 bool is_bridged = app_ns->IsBridged();
Jiyong Park85377812019-05-04 00:30:23 +0900229
230 auto platform_ns = NativeLoaderNamespace::GetPlatformNamespace(is_bridged);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900231 if (!platform_ns) {
232 return platform_ns.error();
233 }
234
235 auto linked = app_ns->Link(*platform_ns, system_exposed_libraries);
236 if (!linked) {
237 return linked.error();
Jiyong Park85377812019-05-04 00:30:23 +0900238 }
239
240 auto runtime_ns = NativeLoaderNamespace::GetExportedNamespace(kRuntimeNamespaceName, is_bridged);
241 // Runtime apex does not exist in host, and under certain build conditions.
Jiyong Park8f4afc82019-07-22 14:20:40 +0900242 if (runtime_ns) {
243 linked = app_ns->Link(*runtime_ns, runtime_public_libraries());
244 if (!linked) {
245 return linked.error();
Jiyong Park85377812019-05-04 00:30:23 +0900246 }
247 }
248
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100249 // Give access to NNAPI libraries (apex-updated LLNDK library).
250 auto nnapi_ns =
251 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900252 if (nnapi_ns) {
253 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
254 if (!linked) {
255 return linked.error();
256 }
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100257 }
258
Jiyong Park85377812019-05-04 00:30:23 +0900259 // Give access to VNDK-SP libraries from the 'vndk' namespace.
260 if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
261 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900262 if (vndk_ns) {
263 linked = app_ns->Link(*vndk_ns, vndksp_libraries());
264 if (!linked) {
265 return linked.error();
266 }
Jiyong Park85377812019-05-04 00:30:23 +0900267 }
268 }
269
Jiyong Park85377812019-05-04 00:30:23 +0900270 if (!vendor_public_libraries().empty()) {
271 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900272 // when vendor_ns is not configured, link to the platform namespace
273 auto target_ns = vendor_ns ? vendor_ns : platform_ns;
274 if (target_ns) {
275 linked = app_ns->Link(*target_ns, vendor_public_libraries());
276 if (!linked) {
277 return linked.error();
278 }
Jiyong Park85377812019-05-04 00:30:23 +0900279 }
280 }
281
Jiyong Park8f4afc82019-07-22 14:20:40 +0900282 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Jiyong Parkb37c4812019-05-16 21:03:30 +0900283 if (is_main_classloader) {
284 app_main_namespace_ = &(*app_ns);
285 }
Jiyong Park6291da22019-04-26 18:55:48 +0900286
287 return &(namespaces_.back().second);
288}
289
290NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
291 jobject class_loader) {
292 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
293 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
294 return env->IsSameObject(value.first, class_loader);
295 });
296 if (it != namespaces_.end()) {
297 return &it->second;
298 }
299
300 return nullptr;
301}
302
Jiyong Park6291da22019-04-26 18:55:48 +0900303NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
304 jobject class_loader) {
305 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
306
307 while (parent_class_loader != nullptr) {
308 NativeLoaderNamespace* ns;
309 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
310 return ns;
311 }
312
313 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
314 }
315
316 return nullptr;
317}
318
Jiyong Park40a60772019-05-03 16:21:31 +0900319} // namespace android::nativeloader