| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 |  | 
|  | 17 | #include "nativeloader/native_loader.h" | 
|  | 18 | #include "ScopedUtfChars.h" | 
|  | 19 |  | 
|  | 20 | #include <dlfcn.h> | 
|  | 21 | #ifdef __ANDROID__ | 
|  | 22 | #include <android/dlext.h> | 
|  | 23 | #include "cutils/properties.h" | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 24 | #define LOG_TAG "libnativeloader" | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 25 | #include "log/log.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 26 | #endif | 
|  | 27 |  | 
|  | 28 | #include <algorithm> | 
|  | 29 | #include <vector> | 
|  | 30 | #include <string> | 
|  | 31 | #include <mutex> | 
|  | 32 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 33 | #include "android-base/file.h" | 
| Elliott Hughes | 8b04714 | 2015-12-08 10:38:59 -0800 | [diff] [blame] | 34 | #include "android-base/macros.h" | 
|  | 35 | #include "android-base/strings.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | namespace android { | 
|  | 38 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 39 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 40 | static constexpr const char* kPublicNativeLibrariesSystemConfig = "/system/etc/public.libraries.txt"; | 
|  | 41 | static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt"; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 42 |  | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 43 | static bool namespace_workaround_enabled(int32_t target_sdk_version) { | 
| Dimitry Ivanov | 4f8bb25 | 2016-03-29 16:10:02 -0700 | [diff] [blame] | 44 | return target_sdk_version <= 23; | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 47 | class LibraryNamespaces { | 
|  | 48 | public: | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 49 | LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 50 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 51 | android_namespace_t* Create(JNIEnv* env, | 
|  | 52 | jobject class_loader, | 
|  | 53 | bool is_shared, | 
|  | 54 | jstring java_library_path, | 
|  | 55 | jstring java_permitted_path, | 
|  | 56 | int32_t target_sdk_version) { | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 57 | ScopedUtfChars library_path(env, java_library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 58 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 59 | std::string permitted_path; | 
|  | 60 | if (java_permitted_path != nullptr) { | 
|  | 61 | ScopedUtfChars path(env, java_permitted_path); | 
|  | 62 | permitted_path = path.c_str(); | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 63 | } else { | 
|  | 64 | // (http://b/27588281) This is a workaround for apps using custom | 
|  | 65 | // classloaders and calling System.load() with an absolute path which | 
|  | 66 | // is outside of the classloader library search path. | 
|  | 67 | // | 
|  | 68 | // This part effectively allows such a classloader to access anything | 
|  | 69 | // under /data | 
|  | 70 | permitted_path = "/data"; | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 73 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 74 | return nullptr; | 
|  | 75 | } | 
|  | 76 |  | 
| Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 77 | android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 78 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 79 | LOG_ALWAYS_FATAL_IF(ns != nullptr, | 
|  | 80 | "There is already a namespace associated with this classloader"); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 81 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 82 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
|  | 83 | if (is_shared) { | 
|  | 84 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
|  | 85 | } | 
|  | 86 |  | 
| Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 87 | ns = android_create_namespace("classloader-namespace", | 
|  | 88 | nullptr, | 
|  | 89 | library_path.c_str(), | 
|  | 90 | namespace_type, | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 91 | !permitted_path.empty() ? | 
| Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 92 | permitted_path.c_str() : | 
|  | 93 | nullptr); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 94 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 95 | if (ns != nullptr) { | 
|  | 96 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); | 
|  | 97 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 98 |  | 
|  | 99 | return ns; | 
|  | 100 | } | 
|  | 101 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 102 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 103 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
|  | 104 | [&](const std::pair<jweak, android_namespace_t*>& value) { | 
|  | 105 | return env->IsSameObject(value.first, class_loader); | 
|  | 106 | }); | 
|  | 107 | return it != namespaces_.end() ? it->second : nullptr; | 
|  | 108 | } | 
|  | 109 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 110 | void Initialize() { | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 111 | std::vector<std::string> sonames; | 
|  | 112 |  | 
|  | 113 | LOG_ALWAYS_FATAL_IF(!ReadConfig(kPublicNativeLibrariesSystemConfig, &sonames), | 
|  | 114 | "Error reading public native library list from \"%s\": %s", | 
|  | 115 | kPublicNativeLibrariesSystemConfig, strerror(errno)); | 
|  | 116 | // This file is optional, quietly ignore if the file does not exist. | 
|  | 117 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); | 
|  | 118 |  | 
|  | 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 | 
|  | 125 | // For now we rely on CTS test to catch things like this but | 
|  | 126 | // it should probably be addressed in the future. | 
|  | 127 | for (const auto& soname : sonames) { | 
|  | 128 | dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | public_libraries_ = base::Join(sonames, ':'); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | private: | 
|  | 135 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 136 | // Read list of public native libraries from the config file. | 
|  | 137 | std::string file_content; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 138 | if(!base::ReadFileToString(configFile, &file_content)) { | 
|  | 139 | return false; | 
|  | 140 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 141 |  | 
|  | 142 | std::vector<std::string> lines = base::Split(file_content, "\n"); | 
|  | 143 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 144 | for (const auto& line : lines) { | 
|  | 145 | auto trimmed_line = base::Trim(line); | 
|  | 146 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { | 
|  | 147 | continue; | 
|  | 148 | } | 
|  | 149 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 150 | sonames->push_back(trimmed_line); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 153 | return true; | 
| Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 154 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 155 |  | 
| Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 156 | bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 157 | std::string publicNativeLibraries = public_libraries_; | 
| Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 158 |  | 
|  | 159 | // TODO (dimitry): This is a workaround for http://b/26436837 | 
|  | 160 | // will be removed before the release. | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 161 | if (namespace_workaround_enabled(target_sdk_version)) { | 
| Dimitry Ivanov | 5f28b84 | 2016-03-04 11:00:37 -0800 | [diff] [blame] | 162 | // check if libart.so is loaded. | 
|  | 163 | void* handle = dlopen("libart.so", RTLD_NOW | RTLD_NOLOAD); | 
|  | 164 | if (handle != nullptr) { | 
|  | 165 | publicNativeLibraries += ":libart.so"; | 
|  | 166 | dlclose(handle); | 
|  | 167 | } | 
| Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 168 | } | 
|  | 169 | // END OF WORKAROUND | 
|  | 170 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 171 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited | 
|  | 172 | // code is one example) unknown to linker in which  case linker uses anonymous | 
|  | 173 | // namespace. The second argument specifies the search path for the anonymous | 
|  | 174 | // namespace which is the library_path of the classloader. | 
| Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 175 | initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 176 |  | 
|  | 177 | return initialized_; | 
|  | 178 | } | 
|  | 179 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 180 | bool initialized_; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 181 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 182 | std::string public_libraries_; | 
|  | 183 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 184 |  | 
|  | 185 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
|  | 186 | }; | 
|  | 187 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 188 | static std::mutex g_namespaces_mutex; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 189 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 190 |  | 
|  | 191 | static bool namespaces_enabled(uint32_t target_sdk_version) { | 
|  | 192 | return target_sdk_version > 0; | 
|  | 193 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 194 | #endif | 
|  | 195 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 196 | void InitializeNativeLoader() { | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 197 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 198 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 199 | g_namespaces->Initialize(); | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 200 | #endif | 
|  | 201 | } | 
|  | 202 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 203 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 204 | jstring CreateClassLoaderNamespace(JNIEnv* env, | 
|  | 205 | int32_t target_sdk_version, | 
|  | 206 | jobject class_loader, | 
|  | 207 | bool is_shared, | 
|  | 208 | jstring library_path, | 
|  | 209 | jstring permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 210 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 211 | if (!namespaces_enabled(target_sdk_version)) { | 
|  | 212 | return nullptr; | 
|  | 213 | } | 
|  | 214 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 215 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 216 | android_namespace_t* ns = g_namespaces->Create(env, | 
|  | 217 | class_loader, | 
|  | 218 | is_shared, | 
|  | 219 | library_path, | 
|  | 220 | permitted_path, | 
|  | 221 | target_sdk_version); | 
|  | 222 | if (ns == nullptr) { | 
|  | 223 | return env->NewStringUTF(dlerror()); | 
|  | 224 | } | 
|  | 225 | #else | 
|  | 226 | UNUSED(env, target_sdk_version, class_loader, is_shared, | 
|  | 227 | library_path, permitted_path); | 
|  | 228 | #endif | 
|  | 229 | return nullptr; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | void* OpenNativeLibrary(JNIEnv* env, | 
|  | 233 | int32_t target_sdk_version, | 
|  | 234 | const char* path, | 
|  | 235 | jobject class_loader, | 
|  | 236 | jstring library_path) { | 
|  | 237 | #if defined(__ANDROID__) | 
|  | 238 | if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 239 | return dlopen(path, RTLD_NOW); | 
|  | 240 | } | 
|  | 241 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 242 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 243 | android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 244 |  | 
|  | 245 | if (ns == nullptr) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 246 | // This is the case where the classloader was not created by ApplicationLoaders | 
|  | 247 | // In this case we create an isolated not-shared namespace for it. | 
|  | 248 | ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version); | 
|  | 249 | if (ns == nullptr) { | 
|  | 250 | return nullptr; | 
|  | 251 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | android_dlextinfo extinfo; | 
|  | 255 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
|  | 256 | extinfo.library_namespace = ns; | 
|  | 257 |  | 
|  | 258 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
|  | 259 | #else | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 260 | UNUSED(env, target_sdk_version, class_loader, library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 261 | return dlopen(path, RTLD_NOW); | 
|  | 262 | #endif | 
|  | 263 | } | 
|  | 264 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 265 | #if defined(__ANDROID__) | 
|  | 266 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 267 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 268 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
|  | 269 | } | 
|  | 270 | #endif | 
|  | 271 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 272 | }; //  android namespace |