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 | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 24 | #include "log/log.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <vector> |
| 29 | #include <string> |
| 30 | #include <mutex> |
| 31 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 32 | #include "android-base/file.h" |
Elliott Hughes | 8b04714 | 2015-12-08 10:38:59 -0800 | [diff] [blame] | 33 | #include "android-base/macros.h" |
| 34 | #include "android-base/strings.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 38 | #if defined(__ANDROID__) |
Dimitry Ivanov | 7e8cee8 | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 39 | static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt"; |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 40 | static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt"; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 41 | |
Dimitry Ivanov | 4ddabd0 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 42 | static bool is_debuggable() { |
| 43 | char debuggable[PROP_VALUE_MAX]; |
| 44 | property_get("ro.debuggable", debuggable, "0"); |
| 45 | return std::string(debuggable) == "1"; |
| 46 | } |
| 47 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 48 | class LibraryNamespaces { |
| 49 | public: |
Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 50 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 51 | |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 52 | android_namespace_t* Create(JNIEnv* env, |
| 53 | jobject class_loader, |
| 54 | bool is_shared, |
| 55 | jstring java_library_path, |
| 56 | jstring java_permitted_path) { |
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(); |
| 63 | } |
| 64 | |
| 65 | if (!initialized_ && !InitPublicNamespace(library_path.c_str())) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
| 68 | |
Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 69 | android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 70 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 71 | LOG_ALWAYS_FATAL_IF(ns != nullptr, |
| 72 | "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 73 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 74 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 75 | if (is_shared) { |
| 76 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 77 | } |
| 78 | |
Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 79 | ns = android_create_namespace("classloader-namespace", |
| 80 | nullptr, |
| 81 | library_path.c_str(), |
| 82 | namespace_type, |
| 83 | java_permitted_path != nullptr ? |
| 84 | permitted_path.c_str() : |
| 85 | nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 86 | |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 87 | if (ns != nullptr) { |
| 88 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); |
| 89 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 90 | |
| 91 | return ns; |
| 92 | } |
| 93 | |
Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 94 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 95 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 96 | [&](const std::pair<jweak, android_namespace_t*>& value) { |
| 97 | return env->IsSameObject(value.first, class_loader); |
| 98 | }); |
| 99 | return it != namespaces_.end() ? it->second : nullptr; |
| 100 | } |
| 101 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 102 | void Initialize() { |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 103 | std::vector<std::string> sonames; |
Dimitry Ivanov | 7e8cee8 | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 104 | const char* android_root_env = getenv("ANDROID_ROOT"); |
| 105 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; |
| 106 | std::string public_native_libraries_system_config = |
| 107 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 108 | |
Dimitry Ivanov | 7e8cee8 | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 109 | LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames), |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 110 | "Error reading public native library list from \"%s\": %s", |
Dimitry Ivanov | 7e8cee8 | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 111 | public_native_libraries_system_config.c_str(), strerror(errno)); |
Dimitry Ivanov | 4ddabd0 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 112 | |
| 113 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 114 | // variable to add libraries to the list. This is intended for platform tests only. |
| 115 | if (is_debuggable()) { |
| 116 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 117 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 118 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 119 | std::copy(additional_libs_vector.begin(), |
| 120 | additional_libs_vector.end(), |
| 121 | std::back_inserter(sonames)); |
| 122 | } |
| 123 | } |
| 124 | |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 125 | // This file is optional, quietly ignore if the file does not exist. |
| 126 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); |
| 127 | |
| 128 | // android_init_namespaces() expects all the public libraries |
| 129 | // to be loaded so that they can be found by soname alone. |
| 130 | // |
| 131 | // TODO(dimitry): this is a bit misleading since we do not know |
| 132 | // if the vendor public library is going to be opened from /vendor/lib |
| 133 | // we might as well end up loading them from /system/lib |
| 134 | // For now we rely on CTS test to catch things like this but |
| 135 | // it should probably be addressed in the future. |
| 136 | for (const auto& soname : sonames) { |
| 137 | dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); |
| 138 | } |
| 139 | |
| 140 | public_libraries_ = base::Join(sonames, ':'); |
| 141 | } |
| 142 | |
Dimitry Ivanov | 911472d | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 143 | void Reset() { |
| 144 | namespaces_.clear(); |
| 145 | } |
| 146 | |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 147 | private: |
| 148 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) { |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 149 | // Read list of public native libraries from the config file. |
| 150 | std::string file_content; |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 151 | if(!base::ReadFileToString(configFile, &file_content)) { |
| 152 | return false; |
| 153 | } |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 154 | |
| 155 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 156 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 157 | for (const auto& line : lines) { |
| 158 | auto trimmed_line = base::Trim(line); |
| 159 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 160 | continue; |
| 161 | } |
| 162 | |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 163 | sonames->push_back(trimmed_line); |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dimitry Ivanov | 617f495 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 166 | return true; |
Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 167 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 168 | |
Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 169 | bool InitPublicNamespace(const char* library_path) { |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 170 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 171 | // code is one example) unknown to linker in which case linker uses anonymous |
| 172 | // namespace. The second argument specifies the search path for the anonymous |
| 173 | // namespace which is the library_path of the classloader. |
| 174 | initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 175 | |
| 176 | return initialized_; |
| 177 | } |
| 178 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 179 | bool initialized_; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 180 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 181 | std::string public_libraries_; |
| 182 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 183 | |
| 184 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 185 | }; |
| 186 | |
Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 187 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 188 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
| 189 | #endif |
| 190 | |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 191 | void InitializeNativeLoader() { |
Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 192 | #if defined(__ANDROID__) |
Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 193 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 194 | g_namespaces->Initialize(); |
Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 195 | #endif |
| 196 | } |
| 197 | |
Dimitry Ivanov | 911472d | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 198 | void ResetNativeLoader() { |
| 199 | #if defined(__ANDROID__) |
| 200 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 201 | g_namespaces->Reset(); |
| 202 | #endif |
| 203 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 204 | |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 205 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 206 | int32_t target_sdk_version, |
| 207 | jobject class_loader, |
| 208 | bool is_shared, |
| 209 | jstring library_path, |
| 210 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 211 | #if defined(__ANDROID__) |
Dimitry Ivanov | 213676b | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 212 | UNUSED(target_sdk_version); |
Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 213 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 214 | android_namespace_t* ns = g_namespaces->Create(env, |
| 215 | class_loader, |
| 216 | is_shared, |
| 217 | library_path, |
| 218 | permitted_path); |
| 219 | if (ns == nullptr) { |
| 220 | return env->NewStringUTF(dlerror()); |
| 221 | } |
| 222 | #else |
| 223 | UNUSED(env, target_sdk_version, class_loader, is_shared, |
| 224 | library_path, permitted_path); |
| 225 | #endif |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | void* OpenNativeLibrary(JNIEnv* env, |
| 230 | int32_t target_sdk_version, |
| 231 | const char* path, |
| 232 | jobject class_loader, |
| 233 | jstring library_path) { |
| 234 | #if defined(__ANDROID__) |
Dimitry Ivanov | 213676b | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 235 | UNUSED(target_sdk_version); |
| 236 | if (class_loader == nullptr) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 237 | return dlopen(path, RTLD_NOW); |
| 238 | } |
| 239 | |
Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 240 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 241 | android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 242 | |
| 243 | if (ns == nullptr) { |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 244 | // This is the case where the classloader was not created by ApplicationLoaders |
| 245 | // In this case we create an isolated not-shared namespace for it. |
| 246 | ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr); |
| 247 | if (ns == nullptr) { |
| 248 | return nullptr; |
| 249 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | android_dlextinfo extinfo; |
| 253 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 254 | extinfo.library_namespace = ns; |
| 255 | |
| 256 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 257 | #else |
Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 258 | UNUSED(env, target_sdk_version, class_loader, library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 259 | return dlopen(path, RTLD_NOW); |
| 260 | #endif |
| 261 | } |
| 262 | |
Dimitry Ivanov | 7a1f951 | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 263 | bool CloseNativeLibrary(void* handle) { |
| 264 | return dlclose(handle) == 0; |
| 265 | } |
| 266 | |
Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 267 | #if defined(__ANDROID__) |
| 268 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 269 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 270 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 271 | } |
| 272 | #endif |
| 273 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 274 | }; // android namespace |