| 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 | */ |
| 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 | |
| 32 | #include "nativeloader/dlext_namespaces.h" |
| 33 | #include "public_libraries.h" |
| 34 | #include "utils.h" |
| 35 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 36 | namespace android::nativeloader { |
| 37 | |
| 38 | namespace { |
| 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. |
| 43 | constexpr const char* kVendorNamespaceName = "sphal"; |
| 44 | constexpr const char* kVndkNamespaceName = "vndk"; |
| 45 | constexpr const char* kArtNamespaceName = "art"; |
| 46 | constexpr const char* kNeuralNetworksNamespaceName = "neuralnetworks"; |
| 47 | |
| 48 | // classloader-namespace is a linker namespace that is created for the loaded |
| 49 | // app. To be specific, it is created for the app classloader. When |
| 50 | // System.load() is called from a Java class that is loaded from the |
| 51 | // classloader, the classloader-namespace namespace associated with that |
| 52 | // classloader is selected for dlopen. The namespace is configured so that its |
| 53 | // search path is set to the app-local JNI directory and it is linked to the |
| 54 | // platform namespace with the names of libs listed in the public.libraries.txt. |
| 55 | // This way an app can only load its own JNI libraries along with the public libs. |
| 56 | constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 57 | // Same thing for vendor APKs. |
| 58 | constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
| 59 | |
| 60 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 61 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 62 | // This list includes all directories app is allowed to access this way. |
| 63 | constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 64 | |
| 65 | constexpr const char* kVendorLibPath = "/vendor/" LIB; |
| 66 | constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB; |
| 67 | |
| 68 | const std::regex kVendorDexPathRegex("(^|:)/vendor/"); |
| 69 | const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); |
| 70 | |
| 71 | // 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] | 72 | using ApkOrigin = enum { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 73 | APK_ORIGIN_DEFAULT = 0, |
| 74 | APK_ORIGIN_VENDOR = 1, |
| 75 | APK_ORIGIN_PRODUCT = 2, |
| Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 76 | }; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 77 | |
| 78 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 79 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 80 | jmethodID get_parent = |
| 81 | env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;"); |
| 82 | |
| 83 | return env->CallObjectMethod(class_loader, get_parent); |
| 84 | } |
| 85 | |
| 86 | ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) { |
| 87 | ApkOrigin apk_origin = APK_ORIGIN_DEFAULT; |
| 88 | |
| 89 | if (dex_path != nullptr) { |
| 90 | ScopedUtfChars dex_path_utf_chars(env, dex_path); |
| 91 | |
| 92 | if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) { |
| 93 | apk_origin = APK_ORIGIN_VENDOR; |
| 94 | } |
| 95 | |
| 96 | if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) { |
| 97 | LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR, |
| 98 | "Dex path contains both vendor and product partition : %s", |
| 99 | dex_path_utf_chars.c_str()); |
| 100 | |
| 101 | apk_origin = APK_ORIGIN_PRODUCT; |
| 102 | } |
| 103 | } |
| 104 | return apk_origin; |
| 105 | } |
| 106 | |
| 107 | } // namespace |
| 108 | |
| 109 | void LibraryNamespaces::Initialize() { |
| 110 | // Once public namespace is initialized there is no |
| 111 | // point in running this code - it will have no effect |
| 112 | // on the current list of public libraries. |
| 113 | if (initialized_) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // android_init_namespaces() expects all the public libraries |
| 118 | // to be loaded so that they can be found by soname alone. |
| 119 | // |
| 120 | // TODO(dimitry): this is a bit misleading since we do not know |
| 121 | // if the vendor public library is going to be opened from /vendor/lib |
| 122 | // we might as well end up loading them from /system/lib or /product/lib |
| 123 | // For now we rely on CTS test to catch things like this but |
| 124 | // it should probably be addressed in the future. |
| 125 | for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) { |
| 126 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 127 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version, |
| 132 | jobject class_loader, bool is_shared, |
| 133 | jstring dex_path, |
| 134 | jstring java_library_path, |
| 135 | jstring java_permitted_path) { |
| 136 | std::string library_path; // empty string by default. |
| 137 | |
| 138 | if (java_library_path != nullptr) { |
| 139 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 140 | library_path = library_path_utf_chars.c_str(); |
| 141 | } |
| 142 | |
| 143 | ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path); |
| 144 | |
| 145 | // (http://b/27588281) This is a workaround for apps using custom |
| 146 | // classloaders and calling System.load() with an absolute path which |
| 147 | // is outside of the classloader library search path. |
| 148 | // |
| 149 | // This part effectively allows such a classloader to access anything |
| 150 | // under /data and /mnt/expand |
| 151 | std::string permitted_path = kWhitelistedDirectories; |
| 152 | |
| 153 | if (java_permitted_path != nullptr) { |
| 154 | ScopedUtfChars path(env, java_permitted_path); |
| 155 | if (path.c_str() != nullptr && path.size() > 0) { |
| 156 | permitted_path = permitted_path + ":" + path.c_str(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr, |
| 161 | "There is already a namespace associated with this classloader"); |
| 162 | |
| 163 | std::string system_exposed_libraries = default_public_libraries(); |
| 164 | const char* namespace_name = kClassloaderNamespaceName; |
| 165 | bool unbundled_vendor_or_product_app = false; |
| 166 | if ((apk_origin == APK_ORIGIN_VENDOR || |
| 167 | (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) && |
| 168 | !is_shared) { |
| 169 | unbundled_vendor_or_product_app = true; |
| 170 | // For vendor / product apks, give access to the vendor / product lib even though |
| 171 | // they are treated as unbundled; the libs and apks are still bundled |
| 172 | // together in the vendor / product partition. |
| 173 | const char* origin_partition; |
| 174 | const char* origin_lib_path; |
| 175 | |
| 176 | switch (apk_origin) { |
| 177 | case APK_ORIGIN_VENDOR: |
| 178 | origin_partition = "vendor"; |
| 179 | origin_lib_path = kVendorLibPath; |
| 180 | break; |
| 181 | case APK_ORIGIN_PRODUCT: |
| 182 | origin_partition = "product"; |
| 183 | origin_lib_path = kProductLibPath; |
| 184 | break; |
| 185 | default: |
| 186 | origin_partition = "unknown"; |
| 187 | origin_lib_path = ""; |
| 188 | } |
| 189 | library_path = library_path + ":" + origin_lib_path; |
| 190 | permitted_path = permitted_path + ":" + origin_lib_path; |
| 191 | |
| 192 | // Also give access to LLNDK libraries since they are available to vendors |
| 193 | system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries().c_str(); |
| 194 | |
| 195 | // Different name is useful for debugging |
| 196 | namespace_name = kVendorClassloaderNamespaceName; |
| 197 | ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s", |
| 198 | origin_partition, library_path.c_str()); |
| 199 | } else { |
| 200 | // extended public libraries are NOT available to vendor apks, otherwise it |
| 201 | // would be system->vendor violation. |
| 202 | if (!extended_public_libraries().empty()) { |
| 203 | system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Create the app namespace |
| 208 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
| 209 | // Heuristic: the first classloader with non-empty library_path is assumed to |
| 210 | // be the main classloader for app |
| 211 | // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its |
| 212 | // friends) and then passing it down to here. |
| 213 | bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty(); |
| 214 | // Policy: the namespace for the main classloader is also used as the |
| 215 | // anonymous namespace. |
| 216 | bool also_used_as_anonymous = is_main_classloader; |
| 217 | // Note: this function is executed with g_namespaces_mutex held, thus no |
| 218 | // racing here. |
| 219 | auto app_ns = NativeLoaderNamespace::Create( |
| 220 | namespace_name, library_path, permitted_path, parent_ns, is_shared, |
| 221 | target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous); |
| 222 | if (!app_ns) { |
| 223 | return app_ns.error(); |
| 224 | } |
| 225 | // ... and link to other namespaces to allow access to some public libraries |
| 226 | bool is_bridged = app_ns->IsBridged(); |
| 227 | |
| 228 | auto platform_ns = NativeLoaderNamespace::GetPlatformNamespace(is_bridged); |
| 229 | if (!platform_ns) { |
| 230 | return platform_ns.error(); |
| 231 | } |
| 232 | |
| 233 | auto linked = app_ns->Link(*platform_ns, system_exposed_libraries); |
| 234 | if (!linked) { |
| 235 | return linked.error(); |
| 236 | } |
| 237 | |
| 238 | auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged); |
| 239 | // ART APEX does not exist on host, and under certain build conditions. |
| 240 | if (art_ns) { |
| 241 | linked = app_ns->Link(*art_ns, art_public_libraries()); |
| 242 | if (!linked) { |
| 243 | return linked.error(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Give access to NNAPI libraries (apex-updated LLNDK library). |
| 248 | auto nnapi_ns = |
| 249 | NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged); |
| 250 | if (nnapi_ns) { |
| 251 | linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries()); |
| 252 | if (!linked) { |
| 253 | return linked.error(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Give access to VNDK-SP libraries from the 'vndk' namespace. |
| 258 | if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) { |
| 259 | auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged); |
| 260 | if (vndk_ns) { |
| 261 | linked = app_ns->Link(*vndk_ns, vndksp_libraries()); |
| 262 | if (!linked) { |
| 263 | return linked.error(); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (!vendor_public_libraries().empty()) { |
| 269 | auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged); |
| 270 | // when vendor_ns is not configured, link to the platform namespace |
| 271 | auto target_ns = vendor_ns ? vendor_ns : platform_ns; |
| 272 | if (target_ns) { |
| 273 | linked = app_ns->Link(*target_ns, vendor_public_libraries()); |
| 274 | if (!linked) { |
| 275 | return linked.error(); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns)); |
| 281 | if (is_main_classloader) { |
| 282 | app_main_namespace_ = &(*app_ns); |
| 283 | } |
| 284 | |
| 285 | return &(namespaces_.back().second); |
| 286 | } |
| 287 | |
| 288 | NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env, |
| 289 | jobject class_loader) { |
| 290 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 291 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
| 292 | return env->IsSameObject(value.first, class_loader); |
| 293 | }); |
| 294 | if (it != namespaces_.end()) { |
| 295 | return &it->second; |
| 296 | } |
| 297 | |
| 298 | return nullptr; |
| 299 | } |
| 300 | |
| 301 | NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env, |
| 302 | jobject class_loader) { |
| 303 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 304 | |
| 305 | while (parent_class_loader != nullptr) { |
| 306 | NativeLoaderNamespace* ns; |
| 307 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { |
| 308 | return ns; |
| 309 | } |
| 310 | |
| 311 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 312 | } |
| 313 | |
| 314 | return nullptr; |
| 315 | } |
| 316 | |
| 317 | } // namespace android::nativeloader |