Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 16 | |
| 17 | #if defined(ART_TARGET_ANDROID) |
| 18 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 19 | #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> |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 31 | #include <android-base/result.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 32 | #include <android-base/strings.h> |
Orion Hodson | 6dc0a43 | 2020-02-06 14:28:28 +0000 | [diff] [blame] | 33 | #include <nativehelper/scoped_utf_chars.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 34 | |
| 35 | #include "nativeloader/dlext_namespaces.h" |
| 36 | #include "public_libraries.h" |
| 37 | #include "utils.h" |
| 38 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 39 | namespace android::nativeloader { |
| 40 | |
| 41 | namespace { |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 42 | |
| 43 | constexpr const char* kApexPath = "/apex/"; |
| 44 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 45 | // The device may be configured to have the vendor libraries loaded to a separate namespace. |
| 46 | // For historical reasons this namespace was named sphal but effectively it is intended |
| 47 | // to use to load vendor libraries to separate namespace with controlled interface between |
| 48 | // vendor and system namespaces. |
| 49 | constexpr const char* kVendorNamespaceName = "sphal"; |
Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 50 | // Similar to sphal namespace, product namespace provides some product libraries. |
| 51 | constexpr const char* kProductNamespaceName = "product"; |
| 52 | |
| 53 | // vndk namespace for unbundled vendor apps |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 54 | constexpr const char* kVndkNamespaceName = "vndk"; |
Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 55 | // vndk_product namespace for unbundled product apps |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 56 | constexpr const char* kVndkProductNamespaceName = "vndk_product"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 57 | |
| 58 | // classloader-namespace is a linker namespace that is created for the loaded |
| 59 | // app. To be specific, it is created for the app classloader. When |
| 60 | // System.load() is called from a Java class that is loaded from the |
| 61 | // classloader, the classloader-namespace namespace associated with that |
| 62 | // classloader is selected for dlopen. The namespace is configured so that its |
| 63 | // search path is set to the app-local JNI directory and it is linked to the |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 64 | // system namespace with the names of libs listed in the public.libraries.txt. |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 65 | // This way an app can only load its own JNI libraries along with the public libs. |
| 66 | constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 67 | // Same thing for vendor APKs. |
| 68 | constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 69 | // If the namespace is shared then add this suffix to form |
| 70 | // "classloader-namespace-shared" or "vendor-classloader-namespace-shared", |
| 71 | // respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has |
| 72 | // inherited all the libraries of the parent classloader namespace, or the |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 73 | // system namespace for the main app classloader. It is used to give full |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 74 | // access to the platform libraries for apps bundled in the system image, |
| 75 | // including their later updates installed in /data. |
| 76 | constexpr const char* kSharedNamespaceSuffix = "-shared"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 77 | |
| 78 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 79 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 80 | // This list includes all directories app is allowed to access this way. |
Orion Hodson | 611d71c | 2021-08-27 17:00:10 +0100 | [diff] [blame] | 81 | constexpr const char* kAlwaysPermittedDirectories = "/data:/mnt/expand"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 82 | |
| 83 | constexpr const char* kVendorLibPath = "/vendor/" LIB; |
| 84 | constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB; |
| 85 | |
| 86 | const std::regex kVendorDexPathRegex("(^|:)/vendor/"); |
| 87 | const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); |
| 88 | |
| 89 | // Define origin of APK if it is from vendor partition or product partition |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 90 | using ApkOrigin = enum { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 91 | APK_ORIGIN_DEFAULT = 0, |
| 92 | APK_ORIGIN_VENDOR = 1, |
| 93 | APK_ORIGIN_PRODUCT = 2, |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 94 | }; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 95 | |
| 96 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 97 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 98 | jmethodID get_parent = |
| 99 | env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;"); |
| 100 | |
| 101 | return env->CallObjectMethod(class_loader, get_parent); |
| 102 | } |
| 103 | |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 104 | ApkOrigin GetApkOriginFromDexPath(const std::string& dex_path) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 105 | ApkOrigin apk_origin = APK_ORIGIN_DEFAULT; |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 106 | if (std::regex_search(dex_path, kVendorDexPathRegex)) { |
| 107 | apk_origin = APK_ORIGIN_VENDOR; |
| 108 | } |
| 109 | if (std::regex_search(dex_path, kProductDexPathRegex)) { |
| 110 | LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR, |
| 111 | "Dex path contains both vendor and product partition : %s", |
| 112 | dex_path.c_str()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 113 | |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 114 | apk_origin = APK_ORIGIN_PRODUCT; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 115 | } |
| 116 | return apk_origin; |
| 117 | } |
| 118 | |
| 119 | } // namespace |
| 120 | |
| 121 | void LibraryNamespaces::Initialize() { |
| 122 | // Once public namespace is initialized there is no |
| 123 | // point in running this code - it will have no effect |
| 124 | // on the current list of public libraries. |
| 125 | if (initialized_) { |
| 126 | return; |
| 127 | } |
| 128 | |
Martin Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 129 | // Load the preloadable public libraries. Since libnativeloader is in the |
| 130 | // com_android_art namespace, use OpenSystemLibrary rather than dlopen to |
| 131 | // ensure the libraries are loaded in the system namespace. |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 132 | // |
| 133 | // TODO(dimitry): this is a bit misleading since we do not know |
| 134 | // if the vendor public library is going to be opened from /vendor/lib |
| 135 | // we might as well end up loading them from /system/lib or /product/lib |
| 136 | // For now we rely on CTS test to catch things like this but |
| 137 | // it should probably be addressed in the future. |
Martin Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 138 | for (const std::string& soname : android::base::Split(preloadable_public_libraries(), ":")) { |
| 139 | void* handle = OpenSystemLibrary(soname.c_str(), RTLD_NOW | RTLD_NODELETE); |
| 140 | LOG_ALWAYS_FATAL_IF(handle == nullptr, |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 141 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); |
| 142 | } |
| 143 | } |
| 144 | |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 145 | // "ALL" is a magic name that allows all public libraries even when the |
| 146 | // target SDK is > 30. Currently this is used for (Java) shared libraries |
| 147 | // which don't use <uses-native-library> |
| 148 | // TODO(b/142191088) remove this hack |
| 149 | static constexpr const char LIBRARY_ALL[] = "ALL"; |
| 150 | |
| 151 | // Returns the colon-separated list of library names by filtering uses_libraries from |
| 152 | // public_libraries. The returned names will actually be available to the app. If the app is pre-S |
| 153 | // (<= 30), the filtering is not done; the entire public_libraries are provided. |
| 154 | static const std::string filter_public_libraries( |
| 155 | uint32_t target_sdk_version, const std::vector<std::string>& uses_libraries, |
| 156 | const std::string& public_libraries) { |
| 157 | // Apps targeting Android 11 or earlier gets all public libraries |
| 158 | if (target_sdk_version <= 30) { |
| 159 | return public_libraries; |
| 160 | } |
| 161 | if (std::find(uses_libraries.begin(), uses_libraries.end(), LIBRARY_ALL) != |
| 162 | uses_libraries.end()) { |
| 163 | return public_libraries; |
| 164 | } |
| 165 | std::vector<std::string> filtered; |
| 166 | std::vector<std::string> orig = android::base::Split(public_libraries, ":"); |
| 167 | for (const auto& lib : uses_libraries) { |
| 168 | if (std::find(orig.begin(), orig.end(), lib) != orig.end()) { |
| 169 | filtered.emplace_back(lib); |
| 170 | } |
| 171 | } |
| 172 | return android::base::Join(filtered, ":"); |
| 173 | } |
| 174 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 175 | Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version, |
| 176 | jobject class_loader, bool is_shared, |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 177 | jstring dex_path_j, |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 178 | jstring java_library_path, |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 179 | jstring java_permitted_path, |
| 180 | jstring uses_library_list) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 181 | std::string library_path; // empty string by default. |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 182 | std::string dex_path; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 183 | |
| 184 | if (java_library_path != nullptr) { |
| 185 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 186 | library_path = library_path_utf_chars.c_str(); |
| 187 | } |
| 188 | |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 189 | if (dex_path_j != nullptr) { |
| 190 | ScopedUtfChars dex_path_chars(env, dex_path_j); |
| 191 | dex_path = dex_path_chars.c_str(); |
| 192 | } |
| 193 | |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 194 | std::vector<std::string> uses_libraries; |
| 195 | if (uses_library_list != nullptr) { |
| 196 | ScopedUtfChars names(env, uses_library_list); |
| 197 | uses_libraries = android::base::Split(names.c_str(), ":"); |
| 198 | } else { |
| 199 | // uses_library_list could be nullptr when System.loadLibrary is called from a |
| 200 | // custom classloader. In that case, we don't know the list of public |
| 201 | // libraries because we don't know which apk the classloader is for. Only |
| 202 | // choices we can have are 1) allowing all public libs (as before), or 2) |
| 203 | // not allowing all but NDK libs. Here we take #1 because #2 would surprise |
| 204 | // developers unnecessarily. |
| 205 | // TODO(b/142191088) finalize the policy here. We could either 1) allow all |
| 206 | // public libs, 2) disallow any lib, or 3) use the libs that were granted to |
| 207 | // the first (i.e. app main) classloader. |
| 208 | uses_libraries.emplace_back(LIBRARY_ALL); |
| 209 | } |
| 210 | |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 211 | ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 212 | |
| 213 | // (http://b/27588281) This is a workaround for apps using custom |
| 214 | // classloaders and calling System.load() with an absolute path which |
| 215 | // is outside of the classloader library search path. |
| 216 | // |
| 217 | // This part effectively allows such a classloader to access anything |
| 218 | // under /data and /mnt/expand |
Orion Hodson | 611d71c | 2021-08-27 17:00:10 +0100 | [diff] [blame] | 219 | std::string permitted_path = kAlwaysPermittedDirectories; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 220 | |
| 221 | if (java_permitted_path != nullptr) { |
| 222 | ScopedUtfChars path(env, java_permitted_path); |
| 223 | if (path.c_str() != nullptr && path.size() > 0) { |
| 224 | permitted_path = permitted_path + ":" + path.c_str(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr, |
| 229 | "There is already a namespace associated with this classloader"); |
| 230 | |
| 231 | std::string system_exposed_libraries = default_public_libraries(); |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 232 | std::string namespace_name = kClassloaderNamespaceName; |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 233 | ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 234 | if ((apk_origin == APK_ORIGIN_VENDOR || |
Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 235 | (apk_origin == APK_ORIGIN_PRODUCT && |
| 236 | is_product_vndk_version_defined())) && |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 237 | !is_shared) { |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 238 | unbundled_app_origin = apk_origin; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 239 | // For vendor / product apks, give access to the vendor / product lib even though |
| 240 | // they are treated as unbundled; the libs and apks are still bundled |
| 241 | // together in the vendor / product partition. |
| 242 | const char* origin_partition; |
| 243 | const char* origin_lib_path; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 244 | const char* llndk_libraries; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 245 | |
| 246 | switch (apk_origin) { |
| 247 | case APK_ORIGIN_VENDOR: |
| 248 | origin_partition = "vendor"; |
| 249 | origin_lib_path = kVendorLibPath; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 250 | llndk_libraries = llndk_libraries_vendor().c_str(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 251 | break; |
| 252 | case APK_ORIGIN_PRODUCT: |
| 253 | origin_partition = "product"; |
| 254 | origin_lib_path = kProductLibPath; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 255 | llndk_libraries = llndk_libraries_product().c_str(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 256 | break; |
| 257 | default: |
| 258 | origin_partition = "unknown"; |
| 259 | origin_lib_path = ""; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 260 | llndk_libraries = ""; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 261 | } |
| 262 | library_path = library_path + ":" + origin_lib_path; |
| 263 | permitted_path = permitted_path + ":" + origin_lib_path; |
| 264 | |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 265 | // Also give access to LLNDK libraries since they are available to vendor or product |
| 266 | system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 267 | |
| 268 | // Different name is useful for debugging |
| 269 | namespace_name = kVendorClassloaderNamespaceName; |
| 270 | ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s", |
| 271 | origin_partition, library_path.c_str()); |
| 272 | } else { |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 273 | auto libs = filter_public_libraries(target_sdk_version, uses_libraries, |
| 274 | extended_public_libraries()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 275 | // extended public libraries are NOT available to vendor apks, otherwise it |
| 276 | // would be system->vendor violation. |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 277 | if (!libs.empty()) { |
| 278 | system_exposed_libraries = system_exposed_libraries + ':' + libs; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 282 | if (is_shared) { |
| 283 | // Show in the name that the namespace was created as shared, for debugging |
| 284 | // purposes. |
| 285 | namespace_name = namespace_name + kSharedNamespaceSuffix; |
| 286 | } |
| 287 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 288 | // Create the app namespace |
| 289 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
| 290 | // Heuristic: the first classloader with non-empty library_path is assumed to |
| 291 | // be the main classloader for app |
| 292 | // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its |
| 293 | // friends) and then passing it down to here. |
| 294 | bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty(); |
| 295 | // Policy: the namespace for the main classloader is also used as the |
| 296 | // anonymous namespace. |
| 297 | bool also_used_as_anonymous = is_main_classloader; |
| 298 | // Note: this function is executed with g_namespaces_mutex held, thus no |
| 299 | // racing here. |
| 300 | auto app_ns = NativeLoaderNamespace::Create( |
| 301 | namespace_name, library_path, permitted_path, parent_ns, is_shared, |
Ryan Prichard | 232db5e | 2020-08-03 15:32:13 -0700 | [diff] [blame] | 302 | target_sdk_version < 24 /* is_exempt_list_enabled */, also_used_as_anonymous); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 303 | if (!app_ns.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 304 | return app_ns.error(); |
| 305 | } |
| 306 | // ... and link to other namespaces to allow access to some public libraries |
| 307 | bool is_bridged = app_ns->IsBridged(); |
| 308 | |
Martin Stjernholm | f0e99bd | 2020-02-11 22:57:14 +0000 | [diff] [blame] | 309 | auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged); |
| 310 | if (!system_ns.ok()) { |
| 311 | return system_ns.error(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Martin Stjernholm | 2207b7e | 2021-04-28 14:52:01 +0100 | [diff] [blame] | 314 | auto linked = app_ns->Link(&system_ns.value(), system_exposed_libraries); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 315 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 316 | return linked.error(); |
| 317 | } |
| 318 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 319 | for (const auto&[apex_ns_name, public_libs] : apex_public_libraries()) { |
| 320 | auto ns = NativeLoaderNamespace::GetExportedNamespace(apex_ns_name, is_bridged); |
| 321 | // Even if APEX namespace is visible, it may not be available to bridged. |
| 322 | if (ns.ok()) { |
Martin Stjernholm | 2207b7e | 2021-04-28 14:52:01 +0100 | [diff] [blame] | 323 | linked = app_ns->Link(&ns.value(), public_libs); |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 324 | if (!linked.ok()) { |
| 325 | return linked.error(); |
| 326 | } |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 330 | // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps. |
| 331 | if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 332 | auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 333 | if (vndk_ns.ok()) { |
Martin Stjernholm | 2207b7e | 2021-04-28 14:52:01 +0100 | [diff] [blame] | 334 | linked = app_ns->Link(&vndk_ns.value(), vndksp_libraries_vendor()); |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 335 | if (!linked.ok()) { |
| 336 | return linked.error(); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps. |
| 342 | if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) { |
| 343 | auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged); |
| 344 | if (vndk_ns.ok()) { |
Martin Stjernholm | 2207b7e | 2021-04-28 14:52:01 +0100 | [diff] [blame] | 345 | linked = app_ns->Link(&vndk_ns.value(), vndksp_libraries_product()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 346 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 347 | return linked.error(); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Jooyung Han | 180e1e7 | 2021-06-20 17:54:38 +0900 | [diff] [blame] | 352 | for (const std::string& each_jar_path : android::base::Split(dex_path, ":")) { |
| 353 | auto apex_ns_name = FindApexNamespaceName(each_jar_path); |
| 354 | if (apex_ns_name.ok()) { |
| 355 | const auto& jni_libs = apex_jni_libraries(*apex_ns_name); |
| 356 | if (jni_libs != "") { |
| 357 | auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged); |
| 358 | if (apex_ns.ok()) { |
| 359 | linked = app_ns->Link(&apex_ns.value(), jni_libs); |
| 360 | if (!linked.ok()) { |
| 361 | return linked.error(); |
| 362 | } |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Jiyong Park | 14626a7 | 2020-07-02 23:17:58 +0900 | [diff] [blame] | 368 | auto vendor_libs = filter_public_libraries(target_sdk_version, uses_libraries, |
| 369 | vendor_public_libraries()); |
| 370 | if (!vendor_libs.empty()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 371 | auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged); |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 372 | // when vendor_ns is not configured, link to the system namespace |
Martin Stjernholm | f0e99bd | 2020-02-11 22:57:14 +0000 | [diff] [blame] | 373 | auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns; |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 374 | if (target_ns.ok()) { |
Martin Stjernholm | 2207b7e | 2021-04-28 14:52:01 +0100 | [diff] [blame] | 375 | linked = app_ns->Link(&target_ns.value(), vendor_libs); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 376 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 377 | return linked.error(); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 382 | auto product_libs = filter_public_libraries(target_sdk_version, uses_libraries, |
| 383 | product_public_libraries()); |
| 384 | if (!product_libs.empty()) { |
| 385 | auto target_ns = system_ns; |
| 386 | if (is_product_vndk_version_defined()) { |
| 387 | // If ro.product.vndk.version is defined, product namespace provides the product libraries. |
| 388 | target_ns = NativeLoaderNamespace::GetExportedNamespace(kProductNamespaceName, is_bridged); |
| 389 | } |
| 390 | if (target_ns.ok()) { |
| 391 | linked = app_ns->Link(&target_ns.value(), product_libs); |
| 392 | if (!linked.ok()) { |
| 393 | return linked.error(); |
| 394 | } |
| 395 | } else { |
| 396 | // The linkerconfig must have a problem on defining the product namespace in the system |
| 397 | // section. Skip linking product namespace. This will not affect most of the apps. Only the |
| 398 | // apps that requires the product public libraries will fail. |
| 399 | ALOGW("Namespace for product libs not found: %s", target_ns.error().message().c_str()); |
| 400 | } |
| 401 | } |
| 402 | |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 403 | auto& emplaced = namespaces_.emplace_back( |
| 404 | std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns)); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 405 | if (is_main_classloader) { |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 406 | app_main_namespace_ = &emplaced.second; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 407 | } |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 408 | return &emplaced.second; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env, |
| 412 | jobject class_loader) { |
| 413 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 414 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
| 415 | return env->IsSameObject(value.first, class_loader); |
| 416 | }); |
| 417 | if (it != namespaces_.end()) { |
| 418 | return &it->second; |
| 419 | } |
| 420 | |
| 421 | return nullptr; |
| 422 | } |
| 423 | |
| 424 | NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env, |
| 425 | jobject class_loader) { |
| 426 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 427 | |
| 428 | while (parent_class_loader != nullptr) { |
| 429 | NativeLoaderNamespace* ns; |
| 430 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { |
| 431 | return ns; |
| 432 | } |
| 433 | |
| 434 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 435 | } |
| 436 | |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 440 | base::Result<std::string> FindApexNamespaceName(const std::string& location) { |
| 441 | // Lots of implicit assumptions here: we expect `location` to be of the form: |
| 442 | // /apex/modulename/... |
| 443 | // |
| 444 | // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it. |
| 445 | if (android::base::StartsWith(location, kApexPath)) { |
| 446 | size_t start_index = strlen(kApexPath); |
| 447 | size_t slash_index = location.find_first_of('/', start_index); |
| 448 | LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos), |
| 449 | "Error finding namespace of apex: no slash in path %s", location.c_str()); |
| 450 | std::string name = location.substr(start_index, slash_index - start_index); |
| 451 | std::replace(name.begin(), name.end(), '.', '_'); |
| 452 | return name; |
| 453 | } |
| 454 | return base::Error(); |
| 455 | } |
| 456 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 457 | } // namespace android::nativeloader |
Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 458 | |
| 459 | #endif // defined(ART_TARGET_ANDROID) |