| 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__) | 
 | 39 | static constexpr const char* kPublicNativeLibrariesConfig = "/system/etc/public.libraries.txt"; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 40 |  | 
 | 41 | class LibraryNamespaces { | 
 | 42 |  public: | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 43 |   LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 44 |  | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 45 |   android_namespace_t* Create(JNIEnv* env, | 
 | 46 |                               jobject class_loader, | 
 | 47 |                               bool is_shared, | 
 | 48 |                               jstring java_library_path, | 
 | 49 |                               jstring java_permitted_path) { | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 50 |     ScopedUtfChars library_path(env, java_library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 51 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 52 |     std::string permitted_path; | 
 | 53 |     if (java_permitted_path != nullptr) { | 
 | 54 |       ScopedUtfChars path(env, java_permitted_path); | 
 | 55 |       permitted_path = path.c_str(); | 
 | 56 |     } | 
 | 57 |  | 
 | 58 |     if (!initialized_ && !InitPublicNamespace(library_path.c_str())) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 59 |       return nullptr; | 
 | 60 |     } | 
 | 61 |  | 
| Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 62 |     android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 63 |  | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 64 |     LOG_ALWAYS_FATAL_IF(ns != nullptr, | 
 | 65 |                         "There is already a namespace associated with this classloader"); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 66 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 67 |     uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
 | 68 |     if (is_shared) { | 
 | 69 |       namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
 | 70 |     } | 
 | 71 |  | 
| Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 72 |     ns = android_create_namespace("classloader-namespace", | 
 | 73 |                                   nullptr, | 
 | 74 |                                   library_path.c_str(), | 
 | 75 |                                   namespace_type, | 
 | 76 |                                   java_permitted_path != nullptr ? | 
 | 77 |                                       permitted_path.c_str() : | 
 | 78 |                                       nullptr); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 79 |  | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 80 |     if (ns != nullptr) { | 
 | 81 |       namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); | 
 | 82 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 83 |  | 
 | 84 |     return ns; | 
 | 85 |   } | 
 | 86 |  | 
| Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 87 |   android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
 | 88 |     auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
 | 89 |                 [&](const std::pair<jweak, android_namespace_t*>& value) { | 
 | 90 |                   return env->IsSameObject(value.first, class_loader); | 
 | 91 |                 }); | 
 | 92 |     return it != namespaces_.end() ? it->second : nullptr; | 
 | 93 |   } | 
 | 94 |  | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 95 |   void Initialize() { | 
 | 96 |     // Read list of public native libraries from the config file. | 
 | 97 |     std::string file_content; | 
 | 98 |     LOG_ALWAYS_FATAL_IF(!base::ReadFileToString(kPublicNativeLibrariesConfig, &file_content), | 
 | 99 |                         "Error reading public native library list from \"%s\": %s", | 
 | 100 |                         kPublicNativeLibrariesConfig, strerror(errno)); | 
 | 101 |  | 
 | 102 |     std::vector<std::string> lines = base::Split(file_content, "\n"); | 
 | 103 |  | 
 | 104 |     std::vector<std::string> sonames; | 
 | 105 |  | 
 | 106 |     for (const auto& line : lines) { | 
 | 107 |       auto trimmed_line = base::Trim(line); | 
 | 108 |       if (trimmed_line[0] == '#' || trimmed_line.empty()) { | 
 | 109 |         continue; | 
 | 110 |       } | 
 | 111 |  | 
 | 112 |       sonames.push_back(trimmed_line); | 
 | 113 |     } | 
 | 114 |  | 
 | 115 |     public_libraries_ = base::Join(sonames, ':'); | 
 | 116 |  | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 117 |     // android_init_namespaces() expects all the public libraries | 
 | 118 |     // to be loaded so that they can be found by soname alone. | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 119 |     for (const auto& soname : sonames) { | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 120 |       dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 121 |     } | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 122 |   } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 123 |  | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 124 |  private: | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 125 |   bool InitPublicNamespace(const char* library_path) { | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 126 |     // (http://b/25844435) - Some apps call dlopen from generated code (mono jited | 
 | 127 |     // code is one example) unknown to linker in which  case linker uses anonymous | 
 | 128 |     // namespace. The second argument specifies the search path for the anonymous | 
 | 129 |     // namespace which is the library_path of the classloader. | 
 | 130 |     initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 131 |  | 
 | 132 |     return initialized_; | 
 | 133 |   } | 
 | 134 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 135 |   bool initialized_; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 136 |   std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 137 |   std::string public_libraries_; | 
 | 138 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 139 |  | 
 | 140 |   DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
 | 141 | }; | 
 | 142 |  | 
| Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 143 | static std::mutex g_namespaces_mutex; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 144 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 145 |  | 
 | 146 | static bool namespaces_enabled(uint32_t target_sdk_version) { | 
 | 147 |   return target_sdk_version > 0; | 
 | 148 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 149 | #endif | 
 | 150 |  | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 151 | void InitializeNativeLoader() { | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 152 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 153 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | d1fdb98 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 154 |   g_namespaces->Initialize(); | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 155 | #endif | 
 | 156 | } | 
 | 157 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 158 |  | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 159 | jstring CreateClassLoaderNamespace(JNIEnv* env, | 
 | 160 |                                    int32_t target_sdk_version, | 
 | 161 |                                    jobject class_loader, | 
 | 162 |                                    bool is_shared, | 
 | 163 |                                    jstring library_path, | 
 | 164 |                                    jstring permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 165 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 166 |   if (!namespaces_enabled(target_sdk_version)) { | 
 | 167 |     return nullptr; | 
 | 168 |   } | 
 | 169 |  | 
| Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 170 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 171 |   android_namespace_t* ns = g_namespaces->Create(env, | 
 | 172 |                                                  class_loader, | 
 | 173 |                                                  is_shared, | 
 | 174 |                                                  library_path, | 
 | 175 |                                                  permitted_path); | 
 | 176 |   if (ns == nullptr) { | 
 | 177 |     return env->NewStringUTF(dlerror()); | 
 | 178 |   } | 
 | 179 | #else | 
 | 180 |   UNUSED(env, target_sdk_version, class_loader, is_shared, | 
 | 181 |          library_path, permitted_path); | 
 | 182 | #endif | 
 | 183 |   return nullptr; | 
 | 184 | } | 
 | 185 |  | 
 | 186 | void* OpenNativeLibrary(JNIEnv* env, | 
 | 187 |                         int32_t target_sdk_version, | 
 | 188 |                         const char* path, | 
 | 189 |                         jobject class_loader, | 
 | 190 |                         jstring library_path) { | 
 | 191 | #if defined(__ANDROID__) | 
 | 192 |   if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 193 |     return dlopen(path, RTLD_NOW); | 
 | 194 |   } | 
 | 195 |  | 
| Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 196 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 197 |   android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 198 |  | 
 | 199 |   if (ns == nullptr) { | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 200 |     // This is the case where the classloader was not created by ApplicationLoaders | 
 | 201 |     // In this case we create an isolated not-shared namespace for it. | 
 | 202 |     ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr); | 
 | 203 |     if (ns == nullptr) { | 
 | 204 |       return nullptr; | 
 | 205 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 206 |   } | 
 | 207 |  | 
 | 208 |   android_dlextinfo extinfo; | 
 | 209 |   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
 | 210 |   extinfo.library_namespace = ns; | 
 | 211 |  | 
 | 212 |   return android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
 | 213 | #else | 
| Dimitry Ivanov | 4cd0768 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 214 |   UNUSED(env, target_sdk_version, class_loader, library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 215 |   return dlopen(path, RTLD_NOW); | 
 | 216 | #endif | 
 | 217 | } | 
 | 218 |  | 
| Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 219 | #if defined(__ANDROID__) | 
 | 220 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 1dff1aa | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 221 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 222 |   return g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
 | 223 | } | 
 | 224 | #endif | 
 | 225 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 226 | }; //  android namespace |