Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [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 | |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 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 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 32 | #include "nativeloader/dlext_namespaces.h" |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 33 | #include "public_libraries.h" |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 34 | #include "utils.h" |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 35 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 36 | using android::base::Error; |
| 37 | |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 38 | namespace android::nativeloader { |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 39 | |
| 40 | namespace { |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 41 | // 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. |
| 45 | constexpr const char* kVendorNamespaceName = "sphal"; |
| 46 | constexpr const char* kVndkNamespaceName = "vndk"; |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 47 | constexpr const char* kRuntimeNamespaceName = "runtime"; |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 48 | constexpr const char* kNeuralNetworksNamespaceName = "neuralnetworks"; |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 49 | |
| 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. |
| 58 | constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 59 | // Same thing for vendor APKs. |
| 60 | constexpr 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. |
| 65 | constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 66 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 67 | constexpr const char* kVendorLibPath = "/vendor/" LIB; |
| 68 | constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB; |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 69 | |
| 70 | const std::regex kVendorDexPathRegex("(^|:)/vendor/"); |
| 71 | const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); |
| 72 | |
| 73 | // Define origin of APK if it is from vendor partition or product partition |
| 74 | typedef enum { |
| 75 | APK_ORIGIN_DEFAULT = 0, |
| 76 | APK_ORIGIN_VENDOR = 1, |
| 77 | APK_ORIGIN_PRODUCT = 2, |
| 78 | } ApkOrigin; |
| 79 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 80 | jobject 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 | |
| 88 | ApkOrigin 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 | |
| 111 | void 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 119 | // 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 Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 127 | for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) { |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 128 | 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 133 | Result<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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 138 | 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 Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 162 | LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr, |
| 163 | "There is already a namespace associated with this classloader"); |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 164 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 165 | std::string system_exposed_libraries = default_public_libraries(); |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 166 | const char* namespace_name = kClassloaderNamespaceName; |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 167 | bool unbundled_vendor_or_product_app = false; |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 168 | if ((apk_origin == APK_ORIGIN_VENDOR || |
| 169 | (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) && |
| 170 | !is_shared) { |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 171 | unbundled_vendor_or_product_app = true; |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 172 | // 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 191 | 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 Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 195 | system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries().c_str(); |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 196 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 197 | // 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 Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 202 | // extended public libraries are NOT available to vendor apks, otherwise it |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 203 | // would be system->vendor violation. |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 204 | if (!extended_public_libraries().empty()) { |
| 205 | system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries(); |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 206 | } |
| 207 | } |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 208 | |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 209 | // Create the app namespace |
| 210 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
Jiyong Park | b37c481 | 2019-05-16 21:03:30 +0900 | [diff] [blame] | 211 | // 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 Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 224 | if (!app_ns) { |
| 225 | return app_ns.error(); |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 226 | } |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 227 | // ... and link to other namespaces to allow access to some public libraries |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 228 | bool is_bridged = app_ns->IsBridged(); |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 229 | |
| 230 | auto platform_ns = NativeLoaderNamespace::GetPlatformNamespace(is_bridged); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 231 | 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 Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | auto runtime_ns = NativeLoaderNamespace::GetExportedNamespace(kRuntimeNamespaceName, is_bridged); |
| 241 | // Runtime apex does not exist in host, and under certain build conditions. |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 242 | if (runtime_ns) { |
| 243 | linked = app_ns->Link(*runtime_ns, runtime_public_libraries()); |
| 244 | if (!linked) { |
| 245 | return linked.error(); |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 249 | // Give access to NNAPI libraries (apex-updated LLNDK library). |
| 250 | auto nnapi_ns = |
| 251 | NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 252 | if (nnapi_ns) { |
| 253 | linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries()); |
| 254 | if (!linked) { |
| 255 | return linked.error(); |
| 256 | } |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 259 | // 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 Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 262 | if (vndk_ns) { |
| 263 | linked = app_ns->Link(*vndk_ns, vndksp_libraries()); |
| 264 | if (!linked) { |
| 265 | return linked.error(); |
| 266 | } |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 270 | if (!vendor_public_libraries().empty()) { |
| 271 | auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 272 | // 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 Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 282 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns)); |
Jiyong Park | b37c481 | 2019-05-16 21:03:30 +0900 | [diff] [blame] | 283 | if (is_main_classloader) { |
| 284 | app_main_namespace_ = &(*app_ns); |
| 285 | } |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 286 | |
| 287 | return &(namespaces_.back().second); |
| 288 | } |
| 289 | |
| 290 | NativeLoaderNamespace* 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 303 | NativeLoaderNamespace* 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 Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 319 | } // namespace android::nativeloader |