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 | |
| 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" |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | namespace { |
| 36 | using namespace std::string_literals; |
| 37 | |
| 38 | constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] = "/etc/public.libraries.txt"; |
| 39 | constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-"; |
| 40 | constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen = |
| 41 | sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1; |
| 42 | constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt"; |
| 43 | constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen = |
| 44 | sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1; |
| 45 | constexpr const char kPublicNativeLibrariesVendorConfig[] = "/vendor/etc/public.libraries.txt"; |
| 46 | constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] = "/etc/llndk.libraries.txt"; |
| 47 | constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] = "/etc/vndksp.libraries.txt"; |
| 48 | |
| 49 | const std::vector<const std::string> kRuntimePublicLibraries = { |
| 50 | "libicuuc.so", |
| 51 | "libicui18n.so", |
| 52 | }; |
| 53 | |
| 54 | // The device may be configured to have the vendor libraries loaded to a separate namespace. |
| 55 | // For historical reasons this namespace was named sphal but effectively it is intended |
| 56 | // to use to load vendor libraries to separate namespace with controlled interface between |
| 57 | // vendor and system namespaces. |
| 58 | constexpr const char* kVendorNamespaceName = "sphal"; |
| 59 | constexpr const char* kVndkNamespaceName = "vndk"; |
| 60 | constexpr const char* kDefaultNamespaceName = "default"; |
| 61 | constexpr const char* kPlatformNamespaceName = "platform"; |
| 62 | constexpr const char* kRuntimeNamespaceName = "runtime"; |
| 63 | |
| 64 | // classloader-namespace is a linker namespace that is created for the loaded |
| 65 | // app. To be specific, it is created for the app classloader. When |
| 66 | // System.load() is called from a Java class that is loaded from the |
| 67 | // classloader, the classloader-namespace namespace associated with that |
| 68 | // classloader is selected for dlopen. The namespace is configured so that its |
| 69 | // search path is set to the app-local JNI directory and it is linked to the |
| 70 | // platform namespace with the names of libs listed in the public.libraries.txt. |
| 71 | // This way an app can only load its own JNI libraries along with the public libs. |
| 72 | constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 73 | // Same thing for vendor APKs. |
| 74 | constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
| 75 | |
| 76 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 77 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 78 | // This list includes all directories app is allowed to access this way. |
| 79 | constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 80 | |
| 81 | #if defined(__LP64__) |
| 82 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/lib64"; |
| 83 | constexpr const char* kVendorLibPath = "/vendor/lib64"; |
| 84 | constexpr const char* kProductLibPath = "/product/lib64:/system/product/lib64"; |
| 85 | #else |
| 86 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/lib"; |
| 87 | constexpr const char* kVendorLibPath = "/vendor/lib"; |
| 88 | constexpr const char* kProductLibPath = "/product/lib:/system/product/lib"; |
| 89 | #endif |
| 90 | |
| 91 | const std::regex kVendorDexPathRegex("(^|:)/vendor/"); |
| 92 | const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); |
| 93 | |
| 94 | // Define origin of APK if it is from vendor partition or product partition |
| 95 | typedef enum { |
| 96 | APK_ORIGIN_DEFAULT = 0, |
| 97 | APK_ORIGIN_VENDOR = 1, |
| 98 | APK_ORIGIN_PRODUCT = 2, |
| 99 | } ApkOrigin; |
| 100 | |
| 101 | bool is_debuggable() { |
| 102 | bool debuggable = false; |
| 103 | debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
| 104 | return debuggable; |
| 105 | } |
| 106 | |
| 107 | std::string vndk_version_str() { |
| 108 | std::string version = android::base::GetProperty("ro.vndk.version", ""); |
| 109 | if (version != "" && version != "current") { |
| 110 | return "." + version; |
| 111 | } |
| 112 | return ""; |
| 113 | } |
| 114 | |
| 115 | void insert_vndk_version_str(std::string* file_name) { |
| 116 | CHECK(file_name != nullptr); |
| 117 | size_t insert_pos = file_name->find_last_of("."); |
| 118 | if (insert_pos == std::string::npos) { |
| 119 | insert_pos = file_name->length(); |
| 120 | } |
| 121 | file_name->insert(insert_pos, vndk_version_str()); |
| 122 | } |
| 123 | |
| 124 | const std::function<bool(const std::string&, std::string*)> always_true = |
| 125 | [](const std::string&, std::string*) { return true; }; |
| 126 | |
| 127 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 128 | const std::function<bool(const std::string& /* soname */, |
| 129 | std::string* /* error_msg */)>& check_soname, |
| 130 | std::string* error_msg = nullptr) { |
| 131 | // Read list of public native libraries from the config file. |
| 132 | std::string file_content; |
| 133 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 134 | if (error_msg) *error_msg = strerror(errno); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 139 | |
| 140 | for (auto& line : lines) { |
| 141 | auto trimmed_line = base::Trim(line); |
| 142 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 143 | continue; |
| 144 | } |
| 145 | size_t space_pos = trimmed_line.rfind(' '); |
| 146 | if (space_pos != std::string::npos) { |
| 147 | std::string type = trimmed_line.substr(space_pos + 1); |
| 148 | if (type != "32" && type != "64") { |
| 149 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 150 | return false; |
| 151 | } |
| 152 | #if defined(__LP64__) |
| 153 | // Skip 32 bit public library. |
| 154 | if (type == "32") { |
| 155 | continue; |
| 156 | } |
| 157 | #else |
| 158 | // Skip 64 bit public library. |
| 159 | if (type == "64") { |
| 160 | continue; |
| 161 | } |
| 162 | #endif |
| 163 | trimmed_line.resize(space_pos); |
| 164 | } |
| 165 | |
| 166 | if (check_soname(trimmed_line, error_msg)) { |
| 167 | sonames->push_back(trimmed_line); |
| 168 | } else { |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 176 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 177 | if (dir != nullptr) { |
| 178 | // Failing to opening the dir is not an error, which can happen in |
| 179 | // webview_zygote. |
| 180 | while (struct dirent* ent = readdir(dir.get())) { |
| 181 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 182 | continue; |
| 183 | } |
| 184 | const std::string filename(ent->d_name); |
| 185 | if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) && |
| 186 | android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) { |
| 187 | const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen; |
| 188 | const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen; |
| 189 | const std::string company_name = filename.substr(start, end - start); |
| 190 | const std::string config_file_path = dirname + "/"s + filename; |
| 191 | LOG_ALWAYS_FATAL_IF( |
| 192 | company_name.empty(), |
| 193 | "Error extracting company name from public native library list file path \"%s\"", |
| 194 | config_file_path.c_str()); |
| 195 | |
| 196 | std::string error_msg; |
| 197 | |
| 198 | LOG_ALWAYS_FATAL_IF( |
| 199 | !ReadConfig(config_file_path, sonames, |
| 200 | [&company_name](const std::string& soname, std::string* error_msg) { |
| 201 | if (android::base::StartsWith(soname, "lib") && |
| 202 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 203 | return true; |
| 204 | } else { |
| 205 | *error_msg = "Library name \"" + soname + |
| 206 | "\" does not end with the company name: " + company_name + |
| 207 | "."; |
| 208 | return false; |
| 209 | } |
| 210 | }, |
| 211 | &error_msg), |
| 212 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), |
| 213 | error_msg.c_str()); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Remove the public libs in runtime namespace |
| 221 | */ |
| 222 | void removePublicLibsIfExistsInRuntimeApex(std::vector<std::string>& sonames) { |
| 223 | for (const std::string& lib_name : kRuntimePublicLibraries) { |
| 224 | std::string path(kRuntimeApexLibPath); |
| 225 | path.append("/").append(lib_name); |
| 226 | |
| 227 | struct stat s; |
| 228 | // Do nothing if the path in /apex does not exist. |
| 229 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 230 | if (stat(path.c_str(), &s) != 0) { |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | auto it = std::find(sonames.begin(), sonames.end(), lib_name); |
| 235 | if (it != sonames.end()) { |
| 236 | sonames.erase(it); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 242 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 243 | jmethodID get_parent = |
| 244 | env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;"); |
| 245 | |
| 246 | return env->CallObjectMethod(class_loader, get_parent); |
| 247 | } |
| 248 | |
| 249 | ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) { |
| 250 | ApkOrigin apk_origin = APK_ORIGIN_DEFAULT; |
| 251 | |
| 252 | if (dex_path != nullptr) { |
| 253 | ScopedUtfChars dex_path_utf_chars(env, dex_path); |
| 254 | |
| 255 | if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) { |
| 256 | apk_origin = APK_ORIGIN_VENDOR; |
| 257 | } |
| 258 | |
| 259 | if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) { |
| 260 | LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR, |
| 261 | "Dex path contains both vendor and product partition : %s", |
| 262 | dex_path_utf_chars.c_str()); |
| 263 | |
| 264 | apk_origin = APK_ORIGIN_PRODUCT; |
| 265 | } |
| 266 | } |
| 267 | return apk_origin; |
| 268 | } |
| 269 | |
| 270 | } // namespace |
| 271 | |
| 272 | void LibraryNamespaces::Initialize() { |
| 273 | // Once public namespace is initialized there is no |
| 274 | // point in running this code - it will have no effect |
| 275 | // on the current list of public libraries. |
| 276 | if (initialized_) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | std::vector<std::string> sonames; |
| 281 | const char* android_root_env = getenv("ANDROID_ROOT"); |
| 282 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; |
| 283 | std::string public_native_libraries_system_config = |
| 284 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; |
| 285 | std::string runtime_public_libraries = base::Join(kRuntimePublicLibraries, ":"); |
| 286 | std::string llndk_native_libraries_system_config = |
| 287 | root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot; |
| 288 | std::string vndksp_native_libraries_system_config = |
| 289 | root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot; |
| 290 | |
| 291 | std::string product_public_native_libraries_dir = "/product/etc"; |
| 292 | |
| 293 | std::string error_msg; |
| 294 | LOG_ALWAYS_FATAL_IF( |
| 295 | !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg), |
| 296 | "Error reading public native library list from \"%s\": %s", |
| 297 | public_native_libraries_system_config.c_str(), error_msg.c_str()); |
| 298 | |
| 299 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 300 | // variable to add libraries to the list. This is intended for platform tests only. |
| 301 | if (is_debuggable()) { |
| 302 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 303 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 304 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 305 | std::copy(additional_libs_vector.begin(), additional_libs_vector.end(), |
| 306 | std::back_inserter(sonames)); |
| 307 | // Apply the same list to the runtime namespace, since some libraries |
| 308 | // might reside there. |
| 309 | CHECK(sizeof(kRuntimePublicLibraries) > 0); |
| 310 | runtime_public_libraries = runtime_public_libraries + ':' + additional_libs; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Remove the public libs in the runtime namespace. |
| 315 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 316 | // in default namespace to dlopen the libs. |
| 317 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. |
| 318 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 319 | // stable symbols in libandroidicu.so. http://b/120786417 |
| 320 | removePublicLibsIfExistsInRuntimeApex(sonames); |
| 321 | |
| 322 | // android_init_namespaces() expects all the public libraries |
| 323 | // to be loaded so that they can be found by soname alone. |
| 324 | // |
| 325 | // TODO(dimitry): this is a bit misleading since we do not know |
| 326 | // if the vendor public library is going to be opened from /vendor/lib |
| 327 | // we might as well end up loading them from /system/lib or /product/lib |
| 328 | // For now we rely on CTS test to catch things like this but |
| 329 | // it should probably be addressed in the future. |
| 330 | for (const auto& soname : sonames) { |
| 331 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 332 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); |
| 333 | } |
| 334 | |
| 335 | system_public_libraries_ = base::Join(sonames, ':'); |
| 336 | runtime_public_libraries_ = runtime_public_libraries; |
| 337 | |
| 338 | // read /system/etc/public.libraries-<companyname>.txt which contain partner defined |
| 339 | // system libs that are exposed to apps. The libs in the txt files must be |
| 340 | // named as lib<name>.<companyname>.so. |
| 341 | sonames.clear(); |
| 342 | ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames); |
| 343 | oem_public_libraries_ = base::Join(sonames, ':'); |
| 344 | |
| 345 | // read /product/etc/public.libraries-<companyname>.txt which contain partner defined |
| 346 | // product libs that are exposed to apps. |
| 347 | sonames.clear(); |
| 348 | ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames); |
| 349 | product_public_libraries_ = base::Join(sonames, ':'); |
| 350 | |
| 351 | // Insert VNDK version to llndk and vndksp config file names. |
| 352 | insert_vndk_version_str(&llndk_native_libraries_system_config); |
| 353 | insert_vndk_version_str(&vndksp_native_libraries_system_config); |
| 354 | |
| 355 | sonames.clear(); |
| 356 | ReadConfig(llndk_native_libraries_system_config, &sonames, always_true); |
| 357 | system_llndk_libraries_ = base::Join(sonames, ':'); |
| 358 | |
| 359 | sonames.clear(); |
| 360 | ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true); |
| 361 | system_vndksp_libraries_ = base::Join(sonames, ':'); |
| 362 | |
| 363 | sonames.clear(); |
| 364 | // This file is optional, quietly ignore if the file does not exist. |
| 365 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr); |
| 366 | |
| 367 | vendor_public_libraries_ = base::Join(sonames, ':'); |
| 368 | } |
| 369 | |
| 370 | NativeLoaderNamespace* LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version, |
| 371 | jobject class_loader, bool is_shared, |
| 372 | jstring dex_path, jstring java_library_path, |
| 373 | jstring java_permitted_path, |
| 374 | std::string* error_msg) { |
| 375 | std::string library_path; // empty string by default. |
| 376 | |
| 377 | if (java_library_path != nullptr) { |
| 378 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 379 | library_path = library_path_utf_chars.c_str(); |
| 380 | } |
| 381 | |
| 382 | ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path); |
| 383 | |
| 384 | // (http://b/27588281) This is a workaround for apps using custom |
| 385 | // classloaders and calling System.load() with an absolute path which |
| 386 | // is outside of the classloader library search path. |
| 387 | // |
| 388 | // This part effectively allows such a classloader to access anything |
| 389 | // under /data and /mnt/expand |
| 390 | std::string permitted_path = kWhitelistedDirectories; |
| 391 | |
| 392 | if (java_permitted_path != nullptr) { |
| 393 | ScopedUtfChars path(env, java_permitted_path); |
| 394 | if (path.c_str() != nullptr && path.size() > 0) { |
| 395 | permitted_path = permitted_path + ":" + path.c_str(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Initialize the anonymous namespace with the first non-empty library path. |
| 400 | if (!library_path.empty() && !initialized_ && |
| 401 | !InitPublicNamespace(library_path.c_str(), error_msg)) { |
| 402 | return nullptr; |
| 403 | } |
| 404 | |
| 405 | bool found = FindNamespaceByClassLoader(env, class_loader); |
| 406 | |
| 407 | LOG_ALWAYS_FATAL_IF(found, "There is already a namespace associated with this classloader"); |
| 408 | |
| 409 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 410 | if (is_shared) { |
| 411 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 412 | } |
| 413 | |
| 414 | if (target_sdk_version < 24) { |
| 415 | namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; |
| 416 | } |
| 417 | |
| 418 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
| 419 | |
| 420 | bool is_native_bridge = false; |
| 421 | |
| 422 | if (parent_ns != nullptr) { |
| 423 | is_native_bridge = !parent_ns->is_android_namespace(); |
| 424 | } else if (!library_path.empty()) { |
| 425 | is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); |
| 426 | } |
| 427 | |
| 428 | std::string system_exposed_libraries = system_public_libraries_; |
| 429 | const char* namespace_name = kClassloaderNamespaceName; |
| 430 | android_namespace_t* vndk_ns = nullptr; |
| 431 | if ((apk_origin == APK_ORIGIN_VENDOR || |
| 432 | (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) && |
| 433 | !is_shared) { |
| 434 | LOG_FATAL_IF(is_native_bridge, |
| 435 | "Unbundled vendor / product apk must not use translated architecture"); |
| 436 | |
| 437 | // For vendor / product apks, give access to the vendor / product lib even though |
| 438 | // they are treated as unbundled; the libs and apks are still bundled |
| 439 | // together in the vendor / product partition. |
| 440 | const char* origin_partition; |
| 441 | const char* origin_lib_path; |
| 442 | |
| 443 | switch (apk_origin) { |
| 444 | case APK_ORIGIN_VENDOR: |
| 445 | origin_partition = "vendor"; |
| 446 | origin_lib_path = kVendorLibPath; |
| 447 | break; |
| 448 | case APK_ORIGIN_PRODUCT: |
| 449 | origin_partition = "product"; |
| 450 | origin_lib_path = kProductLibPath; |
| 451 | break; |
| 452 | default: |
| 453 | origin_partition = "unknown"; |
| 454 | origin_lib_path = ""; |
| 455 | } |
| 456 | |
| 457 | LOG_FATAL_IF(is_native_bridge, "Unbundled %s apk must not use translated architecture", |
| 458 | origin_partition); |
| 459 | |
| 460 | library_path = library_path + ":" + origin_lib_path; |
| 461 | permitted_path = permitted_path + ":" + origin_lib_path; |
| 462 | |
| 463 | // Also give access to LLNDK libraries since they are available to vendors |
| 464 | system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str(); |
| 465 | |
| 466 | // Give access to VNDK-SP libraries from the 'vndk' namespace. |
| 467 | vndk_ns = android_get_exported_namespace(kVndkNamespaceName); |
| 468 | if (vndk_ns == nullptr) { |
| 469 | ALOGW("Cannot find \"%s\" namespace for %s apks", kVndkNamespaceName, origin_partition); |
| 470 | } |
| 471 | |
| 472 | // Different name is useful for debugging |
| 473 | namespace_name = kVendorClassloaderNamespaceName; |
| 474 | ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s", |
| 475 | origin_partition, library_path.c_str()); |
| 476 | } else { |
| 477 | // oem and product public libraries are NOT available to vendor apks, otherwise it |
| 478 | // would be system->vendor violation. |
| 479 | if (!oem_public_libraries_.empty()) { |
| 480 | system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_; |
| 481 | } |
| 482 | if (!product_public_libraries_.empty()) { |
| 483 | system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_; |
| 484 | } |
| 485 | } |
| 486 | std::string runtime_exposed_libraries = runtime_public_libraries_; |
| 487 | |
| 488 | NativeLoaderNamespace native_loader_ns; |
| 489 | if (!is_native_bridge) { |
| 490 | // The platform namespace is called "default" for binaries in /system and |
| 491 | // "platform" for those in the Runtime APEX. Try "platform" first since |
| 492 | // "default" always exists. |
| 493 | android_namespace_t* platform_ns = android_get_exported_namespace(kPlatformNamespaceName); |
| 494 | if (platform_ns == nullptr) { |
| 495 | platform_ns = android_get_exported_namespace(kDefaultNamespaceName); |
| 496 | } |
| 497 | |
| 498 | android_namespace_t* android_parent_ns; |
| 499 | if (parent_ns != nullptr) { |
| 500 | android_parent_ns = parent_ns->get_android_ns(); |
| 501 | } else { |
| 502 | // Fall back to the platform namespace if no parent is found. |
| 503 | android_parent_ns = platform_ns; |
| 504 | } |
| 505 | |
| 506 | android_namespace_t* ns = |
| 507 | android_create_namespace(namespace_name, nullptr, library_path.c_str(), namespace_type, |
| 508 | permitted_path.c_str(), android_parent_ns); |
| 509 | if (ns == nullptr) { |
| 510 | *error_msg = dlerror(); |
| 511 | return nullptr; |
| 512 | } |
| 513 | |
| 514 | // Note that when vendor_ns is not configured this function will return nullptr |
| 515 | // and it will result in linking vendor_public_libraries_ to the default namespace |
| 516 | // which is expected behavior in this case. |
| 517 | android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName); |
| 518 | |
| 519 | android_namespace_t* runtime_ns = android_get_exported_namespace(kRuntimeNamespaceName); |
| 520 | |
| 521 | if (!android_link_namespaces(ns, platform_ns, system_exposed_libraries.c_str())) { |
| 522 | *error_msg = dlerror(); |
| 523 | return nullptr; |
| 524 | } |
| 525 | |
| 526 | // Runtime apex does not exist in host, and under certain build conditions. |
| 527 | if (runtime_ns != nullptr) { |
| 528 | if (!android_link_namespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) { |
| 529 | *error_msg = dlerror(); |
| 530 | return nullptr; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) { |
| 535 | // vendor apks are allowed to use VNDK-SP libraries. |
| 536 | if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) { |
| 537 | *error_msg = dlerror(); |
| 538 | return nullptr; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (!vendor_public_libraries_.empty()) { |
| 543 | if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 544 | *error_msg = dlerror(); |
| 545 | return nullptr; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | native_loader_ns = NativeLoaderNamespace(ns); |
| 550 | } else { |
| 551 | // Same functionality as in the branch above, but calling through native bridge. |
| 552 | |
| 553 | native_bridge_namespace_t* platform_ns = |
| 554 | NativeBridgeGetExportedNamespace(kPlatformNamespaceName); |
| 555 | if (platform_ns == nullptr) { |
| 556 | platform_ns = NativeBridgeGetExportedNamespace(kDefaultNamespaceName); |
| 557 | } |
| 558 | |
| 559 | native_bridge_namespace_t* native_bridge_parent_namespace; |
| 560 | if (parent_ns != nullptr) { |
| 561 | native_bridge_parent_namespace = parent_ns->get_native_bridge_ns(); |
| 562 | } else { |
| 563 | native_bridge_parent_namespace = platform_ns; |
| 564 | } |
| 565 | |
| 566 | native_bridge_namespace_t* ns = |
| 567 | NativeBridgeCreateNamespace(namespace_name, nullptr, library_path.c_str(), namespace_type, |
| 568 | permitted_path.c_str(), native_bridge_parent_namespace); |
| 569 | if (ns == nullptr) { |
| 570 | *error_msg = NativeBridgeGetError(); |
| 571 | return nullptr; |
| 572 | } |
| 573 | |
| 574 | native_bridge_namespace_t* vendor_ns = NativeBridgeGetExportedNamespace(kVendorNamespaceName); |
| 575 | native_bridge_namespace_t* runtime_ns = NativeBridgeGetExportedNamespace(kRuntimeNamespaceName); |
| 576 | |
| 577 | if (!NativeBridgeLinkNamespaces(ns, platform_ns, system_exposed_libraries.c_str())) { |
| 578 | *error_msg = NativeBridgeGetError(); |
| 579 | return nullptr; |
| 580 | } |
| 581 | |
| 582 | // Runtime apex does not exist in host, and under certain build conditions. |
| 583 | if (runtime_ns != nullptr) { |
| 584 | if (!NativeBridgeLinkNamespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) { |
| 585 | *error_msg = NativeBridgeGetError(); |
| 586 | return nullptr; |
| 587 | } |
| 588 | } |
| 589 | if (!vendor_public_libraries_.empty()) { |
| 590 | if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 591 | *error_msg = NativeBridgeGetError(); |
| 592 | return nullptr; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | native_loader_ns = NativeLoaderNamespace(ns); |
| 597 | } |
| 598 | |
| 599 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); |
| 600 | |
| 601 | return &(namespaces_.back().second); |
| 602 | } |
| 603 | |
| 604 | NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env, |
| 605 | jobject class_loader) { |
| 606 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 607 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
| 608 | return env->IsSameObject(value.first, class_loader); |
| 609 | }); |
| 610 | if (it != namespaces_.end()) { |
| 611 | return &it->second; |
| 612 | } |
| 613 | |
| 614 | return nullptr; |
| 615 | } |
| 616 | |
| 617 | bool LibraryNamespaces::InitPublicNamespace(const char* library_path, std::string* error_msg) { |
| 618 | // Ask native bride if this apps library path should be handled by it |
| 619 | bool is_native_bridge = NativeBridgeIsPathSupported(library_path); |
| 620 | |
| 621 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 622 | // code is one example) unknown to linker in which case linker uses anonymous |
| 623 | // namespace. The second argument specifies the search path for the anonymous |
| 624 | // namespace which is the library_path of the classloader. |
| 625 | initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(), |
| 626 | is_native_bridge ? nullptr : library_path); |
| 627 | if (!initialized_) { |
| 628 | *error_msg = dlerror(); |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | // and now initialize native bridge namespaces if necessary. |
| 633 | if (NativeBridgeInitialized()) { |
| 634 | initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(), |
| 635 | is_native_bridge ? library_path : nullptr); |
| 636 | if (!initialized_) { |
| 637 | *error_msg = NativeBridgeGetError(); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return initialized_; |
| 642 | } |
| 643 | |
| 644 | NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env, |
| 645 | jobject class_loader) { |
| 646 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 647 | |
| 648 | while (parent_class_loader != nullptr) { |
| 649 | NativeLoaderNamespace* ns; |
| 650 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { |
| 651 | return ns; |
| 652 | } |
| 653 | |
| 654 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 655 | } |
| 656 | |
| 657 | return nullptr; |
| 658 | } |
| 659 | |
| 660 | } // namespace android |