| 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" | 
| Steven Moreland | 00fe3ad | 2017-07-18 16:53:54 -0700 | [diff] [blame] | 18 | #include <nativehelper/ScopedUtfChars.h> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 19 |  | 
 | 20 | #include <dlfcn.h> | 
 | 21 | #ifdef __ANDROID__ | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 22 | #define LOG_TAG "libnativeloader" | 
| Jesse Hall | b75d82b | 2017-01-09 16:04:28 -0800 | [diff] [blame] | 23 | #include "nativeloader/dlext_namespaces.h" | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include "cutils/properties.h" | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include "log/log.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 26 | #endif | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 27 | #include "nativebridge/native_bridge.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 28 |  | 
 | 29 | #include <algorithm> | 
 | 30 | #include <vector> | 
 | 31 | #include <string> | 
 | 32 | #include <mutex> | 
 | 33 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> | 
 | 35 | #include <android-base/macros.h> | 
 | 36 | #include <android-base/strings.h> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 37 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 38 | #define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\ | 
 | 39 |                                              "%s:%d: %s CHECK '" #predicate "' failed.",\ | 
 | 40 |                                              __FILE__, __LINE__, __FUNCTION__) | 
 | 41 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 42 | namespace android { | 
 | 43 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 44 | #if defined(__ANDROID__) | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 45 | class NativeLoaderNamespace { | 
 | 46 |  public: | 
 | 47 |   NativeLoaderNamespace() | 
 | 48 |       : android_ns_(nullptr), native_bridge_ns_(nullptr) { } | 
 | 49 |  | 
 | 50 |   explicit NativeLoaderNamespace(android_namespace_t* ns) | 
 | 51 |       : android_ns_(ns), native_bridge_ns_(nullptr) { } | 
 | 52 |  | 
 | 53 |   explicit NativeLoaderNamespace(native_bridge_namespace_t* ns) | 
 | 54 |       : android_ns_(nullptr), native_bridge_ns_(ns) { } | 
 | 55 |  | 
 | 56 |   NativeLoaderNamespace(NativeLoaderNamespace&& that) = default; | 
 | 57 |   NativeLoaderNamespace(const NativeLoaderNamespace& that) = default; | 
 | 58 |  | 
 | 59 |   NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default; | 
 | 60 |  | 
 | 61 |   android_namespace_t* get_android_ns() const { | 
 | 62 |     CHECK(native_bridge_ns_ == nullptr); | 
 | 63 |     return android_ns_; | 
 | 64 |   } | 
 | 65 |  | 
 | 66 |   native_bridge_namespace_t* get_native_bridge_ns() const { | 
 | 67 |     CHECK(android_ns_ == nullptr); | 
 | 68 |     return native_bridge_ns_; | 
 | 69 |   } | 
 | 70 |  | 
 | 71 |   bool is_android_namespace() const { | 
 | 72 |     return native_bridge_ns_ == nullptr; | 
 | 73 |   } | 
 | 74 |  | 
 | 75 |  private: | 
 | 76 |   // Only one of them can be not null | 
 | 77 |   android_namespace_t* android_ns_; | 
 | 78 |   native_bridge_namespace_t* native_bridge_ns_; | 
 | 79 | }; | 
 | 80 |  | 
 | 81 | static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = | 
 | 82 |                                   "/etc/public.libraries.txt"; | 
 | 83 | static constexpr const char* kPublicNativeLibrariesVendorConfig = | 
 | 84 |                                   "/vendor/etc/public.libraries.txt"; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 85 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 86 | // The device may be configured to have the vendor libraries loaded to a separate namespace. | 
 | 87 | // For historical reasons this namespace was named sphal but effectively it is intended | 
 | 88 | // to use to load vendor libraries to separate namespace with controlled interface between | 
 | 89 | // vendor and system namespaces. | 
 | 90 | static constexpr const char* kVendorNamespaceName = "sphal"; | 
 | 91 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 92 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling | 
 | 93 | // System.load() with an absolute path which is outside of the classloader library search path. | 
 | 94 | // This list includes all directories app is allowed to access this way. | 
 | 95 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; | 
 | 96 |  | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 97 | static bool is_debuggable() { | 
 | 98 |   char debuggable[PROP_VALUE_MAX]; | 
 | 99 |   property_get("ro.debuggable", debuggable, "0"); | 
 | 100 |   return std::string(debuggable) == "1"; | 
 | 101 | } | 
 | 102 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 103 | class LibraryNamespaces { | 
 | 104 |  public: | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 105 |   LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 106 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 107 |   bool Create(JNIEnv* env, | 
| Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 108 |               uint32_t target_sdk_version, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 109 |               jobject class_loader, | 
 | 110 |               bool is_shared, | 
 | 111 |               jstring java_library_path, | 
 | 112 |               jstring java_permitted_path, | 
 | 113 |               NativeLoaderNamespace* ns, | 
 | 114 |               std::string* error_msg) { | 
| Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 115 |     std::string library_path; // empty string by default. | 
 | 116 |  | 
 | 117 |     if (java_library_path != nullptr) { | 
 | 118 |       ScopedUtfChars library_path_utf_chars(env, java_library_path); | 
 | 119 |       library_path = library_path_utf_chars.c_str(); | 
 | 120 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 121 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 122 |     // (http://b/27588281) This is a workaround for apps using custom | 
 | 123 |     // classloaders and calling System.load() with an absolute path which | 
 | 124 |     // is outside of the classloader library search path. | 
 | 125 |     // | 
 | 126 |     // This part effectively allows such a classloader to access anything | 
 | 127 |     // under /data and /mnt/expand | 
 | 128 |     std::string permitted_path = kWhitelistedDirectories; | 
 | 129 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 130 |     if (java_permitted_path != nullptr) { | 
 | 131 |       ScopedUtfChars path(env, java_permitted_path); | 
| Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 132 |       if (path.c_str() != nullptr && path.size() > 0) { | 
 | 133 |         permitted_path = permitted_path + ":" + path.c_str(); | 
 | 134 |       } | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 135 |     } | 
 | 136 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 137 |     if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) { | 
 | 138 |       return false; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 139 |     } | 
 | 140 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 141 |     bool found = FindNamespaceByClassLoader(env, class_loader, nullptr); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 142 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 143 |     LOG_ALWAYS_FATAL_IF(found, | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 144 |                         "There is already a namespace associated with this classloader"); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 145 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 146 |     uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
 | 147 |     if (is_shared) { | 
 | 148 |       namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
 | 149 |     } | 
 | 150 |  | 
| Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 151 |     if (target_sdk_version < 24) { | 
 | 152 |       namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; | 
 | 153 |     } | 
 | 154 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 155 |     NativeLoaderNamespace parent_ns; | 
 | 156 |     bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns); | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 157 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 158 |     bool is_native_bridge = false; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 159 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 160 |     if (found_parent_namespace) { | 
 | 161 |       is_native_bridge = !parent_ns.is_android_namespace(); | 
 | 162 |     } else if (!library_path.empty()) { | 
 | 163 |       is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 164 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 165 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 166 |     NativeLoaderNamespace native_loader_ns; | 
 | 167 |     if (!is_native_bridge) { | 
 | 168 |       android_namespace_t* ns = android_create_namespace("classloader-namespace", | 
 | 169 |                                                          nullptr, | 
 | 170 |                                                          library_path.c_str(), | 
 | 171 |                                                          namespace_type, | 
 | 172 |                                                          permitted_path.c_str(), | 
 | 173 |                                                          parent_ns.get_android_ns()); | 
 | 174 |       if (ns == nullptr) { | 
 | 175 |         *error_msg = dlerror(); | 
 | 176 |         return false; | 
 | 177 |       } | 
 | 178 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 179 |       // Note that when vendor_ns is not configured this function will return nullptr | 
 | 180 |       // and it will result in linking vendor_public_libraries_ to the default namespace | 
 | 181 |       // which is expected behavior in this case. | 
 | 182 |       android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName); | 
 | 183 |  | 
 | 184 |       if (!android_link_namespaces(ns, nullptr, system_public_libraries_.c_str())) { | 
| Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 185 |         *error_msg = dlerror(); | 
 | 186 |         return false; | 
 | 187 |       } | 
 | 188 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 189 |       if (!vendor_public_libraries_.empty()) { | 
 | 190 |         if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { | 
 | 191 |           *error_msg = dlerror(); | 
 | 192 |           return false; | 
 | 193 |         } | 
 | 194 |       } | 
 | 195 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 196 |       native_loader_ns = NativeLoaderNamespace(ns); | 
 | 197 |     } else { | 
 | 198 |       native_bridge_namespace_t* ns = NativeBridgeCreateNamespace("classloader-namespace", | 
 | 199 |                                                                   nullptr, | 
 | 200 |                                                                   library_path.c_str(), | 
 | 201 |                                                                   namespace_type, | 
 | 202 |                                                                   permitted_path.c_str(), | 
 | 203 |                                                                   parent_ns.get_native_bridge_ns()); | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 204 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 205 |       if (ns == nullptr) { | 
 | 206 |         *error_msg = NativeBridgeGetError(); | 
 | 207 |         return false; | 
 | 208 |       } | 
 | 209 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 210 |       native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace(); | 
 | 211 |  | 
 | 212 |       if (!NativeBridgeLinkNamespaces(ns, nullptr, system_public_libraries_.c_str())) { | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 213 |         *error_msg = NativeBridgeGetError(); | 
 | 214 |         return false; | 
 | 215 |       } | 
 | 216 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 217 |       if (!vendor_public_libraries_.empty()) { | 
 | 218 |         if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { | 
 | 219 |           *error_msg = NativeBridgeGetError(); | 
 | 220 |           return false; | 
 | 221 |         } | 
 | 222 |       } | 
 | 223 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 224 |       native_loader_ns = NativeLoaderNamespace(ns); | 
 | 225 |     } | 
 | 226 |  | 
 | 227 |     namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); | 
 | 228 |  | 
 | 229 |     *ns = native_loader_ns; | 
 | 230 |     return true; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 231 |   } | 
 | 232 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 233 |   bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) { | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 234 |     auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 235 |                 [&](const std::pair<jweak, NativeLoaderNamespace>& value) { | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 236 |                   return env->IsSameObject(value.first, class_loader); | 
 | 237 |                 }); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 238 |     if (it != namespaces_.end()) { | 
 | 239 |       if (ns != nullptr) { | 
 | 240 |         *ns = it->second; | 
 | 241 |       } | 
 | 242 |  | 
 | 243 |       return true; | 
 | 244 |     } | 
 | 245 |  | 
 | 246 |     return false; | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 247 |   } | 
 | 248 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 249 |   void Initialize() { | 
| Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 250 |     // Once public namespace is initialized there is no | 
 | 251 |     // point in running this code - it will have no effect | 
 | 252 |     // on the current list of public libraries. | 
 | 253 |     if (initialized_) { | 
 | 254 |       return; | 
 | 255 |     } | 
 | 256 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 257 |     std::vector<std::string> sonames; | 
| Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 258 |     const char* android_root_env = getenv("ANDROID_ROOT"); | 
 | 259 |     std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; | 
 | 260 |     std::string public_native_libraries_system_config = | 
 | 261 |             root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 262 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 263 |     std::string error_msg; | 
 | 264 |     LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames, &error_msg), | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 265 |                         "Error reading public native library list from \"%s\": %s", | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 266 |                         public_native_libraries_system_config.c_str(), error_msg.c_str()); | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 267 |  | 
 | 268 |     // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment | 
 | 269 |     // variable to add libraries to the list. This is intended for platform tests only. | 
 | 270 |     if (is_debuggable()) { | 
 | 271 |       const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); | 
 | 272 |       if (additional_libs != nullptr && additional_libs[0] != '\0') { | 
 | 273 |         std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); | 
 | 274 |         std::copy(additional_libs_vector.begin(), | 
 | 275 |                   additional_libs_vector.end(), | 
 | 276 |                   std::back_inserter(sonames)); | 
 | 277 |       } | 
 | 278 |     } | 
 | 279 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 280 |     // android_init_namespaces() expects all the public libraries | 
 | 281 |     // to be loaded so that they can be found by soname alone. | 
 | 282 |     // | 
 | 283 |     // TODO(dimitry): this is a bit misleading since we do not know | 
 | 284 |     // if the vendor public library is going to be opened from /vendor/lib | 
 | 285 |     // we might as well end up loading them from /system/lib | 
 | 286 |     // For now we rely on CTS test to catch things like this but | 
 | 287 |     // it should probably be addressed in the future. | 
 | 288 |     for (const auto& soname : sonames) { | 
| Evan Ralston | 15a264e | 2017-02-03 17:09:46 -0800 | [diff] [blame] | 289 |       LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, | 
 | 290 |                           "Error preloading public library %s: %s", | 
 | 291 |                           soname.c_str(), dlerror()); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 292 |     } | 
 | 293 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 294 |     system_public_libraries_ = base::Join(sonames, ':'); | 
 | 295 |  | 
 | 296 |     sonames.clear(); | 
 | 297 |     // This file is optional, quietly ignore if the file does not exist. | 
 | 298 |     ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); | 
 | 299 |  | 
 | 300 |     vendor_public_libraries_ = base::Join(sonames, ':'); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 301 |   } | 
 | 302 |  | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 303 |   void Reset() { | 
 | 304 |     namespaces_.clear(); | 
 | 305 |   } | 
 | 306 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 307 |  private: | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 308 |   bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, | 
 | 309 |                   std::string* error_msg = nullptr) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 310 |     // Read list of public native libraries from the config file. | 
 | 311 |     std::string file_content; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 312 |     if(!base::ReadFileToString(configFile, &file_content)) { | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 313 |       if (error_msg) *error_msg = strerror(errno); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 314 |       return false; | 
 | 315 |     } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 316 |  | 
 | 317 |     std::vector<std::string> lines = base::Split(file_content, "\n"); | 
 | 318 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 319 |     for (auto& line : lines) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 320 |       auto trimmed_line = base::Trim(line); | 
 | 321 |       if (trimmed_line[0] == '#' || trimmed_line.empty()) { | 
 | 322 |         continue; | 
 | 323 |       } | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 324 |       size_t space_pos = trimmed_line.rfind(' '); | 
 | 325 |       if (space_pos != std::string::npos) { | 
 | 326 |         std::string type = trimmed_line.substr(space_pos + 1); | 
 | 327 |         if (type != "32" && type != "64") { | 
 | 328 |           if (error_msg) *error_msg = "Malformed line: " + line; | 
 | 329 |           return false; | 
 | 330 |         } | 
 | 331 | #if defined(__LP64__) | 
 | 332 |         // Skip 32 bit public library. | 
 | 333 |         if (type == "32") { | 
 | 334 |           continue; | 
 | 335 |         } | 
 | 336 | #else | 
 | 337 |         // Skip 64 bit public library. | 
 | 338 |         if (type == "64") { | 
 | 339 |           continue; | 
 | 340 |         } | 
 | 341 | #endif | 
 | 342 |         trimmed_line.resize(space_pos); | 
 | 343 |       } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 344 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 345 |       sonames->push_back(trimmed_line); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 346 |     } | 
 | 347 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 348 |     return true; | 
| Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 349 |   } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 350 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 351 |   bool InitPublicNamespace(const char* library_path, std::string* error_msg) { | 
 | 352 |     // Ask native bride if this apps library path should be handled by it | 
 | 353 |     bool is_native_bridge = NativeBridgeIsPathSupported(library_path); | 
 | 354 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 355 |     // (http://b/25844435) - Some apps call dlopen from generated code (mono jited | 
 | 356 |     // code is one example) unknown to linker in which  case linker uses anonymous | 
 | 357 |     // namespace. The second argument specifies the search path for the anonymous | 
 | 358 |     // namespace which is the library_path of the classloader. | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 359 |     initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(), | 
| Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 360 |                                                     is_native_bridge ? nullptr : library_path); | 
| Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 361 |     if (!initialized_) { | 
 | 362 |       *error_msg = dlerror(); | 
 | 363 |       return false; | 
 | 364 |     } | 
 | 365 |  | 
 | 366 |     // and now initialize native bridge namespaces if necessary. | 
 | 367 |     if (NativeBridgeInitialized()) { | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 368 |       initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(), | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 369 |                                                         is_native_bridge ? library_path : nullptr); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 370 |       if (!initialized_) { | 
 | 371 |         *error_msg = NativeBridgeGetError(); | 
 | 372 |       } | 
 | 373 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 374 |  | 
 | 375 |     return initialized_; | 
 | 376 |   } | 
 | 377 |  | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 378 |   jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { | 
 | 379 |     jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); | 
 | 380 |     jmethodID get_parent = env->GetMethodID(class_loader_class, | 
 | 381 |                                             "getParent", | 
 | 382 |                                             "()Ljava/lang/ClassLoader;"); | 
 | 383 |  | 
 | 384 |     return env->CallObjectMethod(class_loader, get_parent); | 
 | 385 |   } | 
 | 386 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 387 |   bool FindParentNamespaceByClassLoader(JNIEnv* env, | 
 | 388 |                                         jobject class_loader, | 
 | 389 |                                         NativeLoaderNamespace* ns) { | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 390 |     jobject parent_class_loader = GetParentClassLoader(env, class_loader); | 
 | 391 |  | 
 | 392 |     while (parent_class_loader != nullptr) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 393 |       if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) { | 
 | 394 |         return true; | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 395 |       } | 
 | 396 |  | 
 | 397 |       parent_class_loader = GetParentClassLoader(env, parent_class_loader); | 
 | 398 |     } | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 399 |  | 
 | 400 |     return false; | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 401 |   } | 
 | 402 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 403 |   bool initialized_; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 404 |   std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_; | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 405 |   std::string system_public_libraries_; | 
 | 406 |   std::string vendor_public_libraries_; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 407 |  | 
 | 408 |   DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
 | 409 | }; | 
 | 410 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 411 | static std::mutex g_namespaces_mutex; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 412 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
 | 413 | #endif | 
 | 414 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 415 | void InitializeNativeLoader() { | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 416 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 417 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 418 |   g_namespaces->Initialize(); | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 419 | #endif | 
 | 420 | } | 
 | 421 |  | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 422 | void ResetNativeLoader() { | 
 | 423 | #if defined(__ANDROID__) | 
 | 424 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
 | 425 |   g_namespaces->Reset(); | 
 | 426 | #endif | 
 | 427 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 428 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 429 | jstring CreateClassLoaderNamespace(JNIEnv* env, | 
 | 430 |                                    int32_t target_sdk_version, | 
 | 431 |                                    jobject class_loader, | 
 | 432 |                                    bool is_shared, | 
 | 433 |                                    jstring library_path, | 
 | 434 |                                    jstring permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 435 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 436 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 437 |  | 
 | 438 |   std::string error_msg; | 
 | 439 |   NativeLoaderNamespace ns; | 
 | 440 |   bool success = g_namespaces->Create(env, | 
| Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 441 |                                       target_sdk_version, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 442 |                                       class_loader, | 
 | 443 |                                       is_shared, | 
 | 444 |                                       library_path, | 
 | 445 |                                       permitted_path, | 
 | 446 |                                       &ns, | 
 | 447 |                                       &error_msg); | 
 | 448 |   if (!success) { | 
 | 449 |     return env->NewStringUTF(error_msg.c_str()); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 450 |   } | 
 | 451 | #else | 
 | 452 |   UNUSED(env, target_sdk_version, class_loader, is_shared, | 
 | 453 |          library_path, permitted_path); | 
 | 454 | #endif | 
 | 455 |   return nullptr; | 
 | 456 | } | 
 | 457 |  | 
 | 458 | void* OpenNativeLibrary(JNIEnv* env, | 
 | 459 |                         int32_t target_sdk_version, | 
 | 460 |                         const char* path, | 
 | 461 |                         jobject class_loader, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 462 |                         jstring library_path, | 
 | 463 |                         bool* needs_native_bridge, | 
 | 464 |                         std::string* error_msg) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 465 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 466 |   UNUSED(target_sdk_version); | 
 | 467 |   if (class_loader == nullptr) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 468 |     *needs_native_bridge = false; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 469 |     return dlopen(path, RTLD_NOW); | 
 | 470 |   } | 
 | 471 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 472 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 473 |   NativeLoaderNamespace ns; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 474 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 475 |   if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 476 |     // This is the case where the classloader was not created by ApplicationLoaders | 
 | 477 |     // In this case we create an isolated not-shared namespace for it. | 
| Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 478 |     if (!g_namespaces->Create(env, | 
 | 479 |                               target_sdk_version, | 
 | 480 |                               class_loader, | 
 | 481 |                               false, | 
 | 482 |                               library_path, | 
 | 483 |                               nullptr, | 
 | 484 |                               &ns, | 
 | 485 |                               error_msg)) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 486 |       return nullptr; | 
 | 487 |     } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 488 |   } | 
 | 489 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 490 |   if (ns.is_android_namespace()) { | 
 | 491 |     android_dlextinfo extinfo; | 
 | 492 |     extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
 | 493 |     extinfo.library_namespace = ns.get_android_ns(); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 494 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 495 |     void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
 | 496 |     if (handle == nullptr) { | 
 | 497 |       *error_msg = dlerror(); | 
 | 498 |     } | 
 | 499 |     *needs_native_bridge = false; | 
 | 500 |     return handle; | 
 | 501 |   } else { | 
 | 502 |     void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns()); | 
 | 503 |     if (handle == nullptr) { | 
 | 504 |       *error_msg = NativeBridgeGetError(); | 
 | 505 |     } | 
 | 506 |     *needs_native_bridge = true; | 
 | 507 |     return handle; | 
 | 508 |   } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 509 | #else | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 510 |   UNUSED(env, target_sdk_version, class_loader, library_path); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 511 |   *needs_native_bridge = false; | 
 | 512 |   void* handle = dlopen(path, RTLD_NOW); | 
 | 513 |   if (handle == nullptr) { | 
 | 514 |     if (NativeBridgeIsSupported(path)) { | 
 | 515 |       *needs_native_bridge = true; | 
 | 516 |       handle = NativeBridgeLoadLibrary(path, RTLD_NOW); | 
 | 517 |       if (handle == nullptr) { | 
 | 518 |         *error_msg = NativeBridgeGetError(); | 
 | 519 |       } | 
 | 520 |     } else { | 
 | 521 |       *needs_native_bridge = false; | 
 | 522 |       *error_msg = dlerror(); | 
 | 523 |     } | 
 | 524 |   } | 
 | 525 |   return handle; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 526 | #endif | 
 | 527 | } | 
 | 528 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 529 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) { | 
 | 530 |     return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) : | 
 | 531 |                                  dlclose(handle); | 
| Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 532 | } | 
 | 533 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 534 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 535 | // native_bridge_namespaces are not supported for callers of this function. | 
 | 536 | // This function will return nullptr in the case when application is running | 
 | 537 | // on native bridge. | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 538 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 539 |   std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 540 |   NativeLoaderNamespace ns; | 
 | 541 |   if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { | 
| Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 542 |     return ns.is_android_namespace() ? ns.get_android_ns() : nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 543 |   } | 
 | 544 |  | 
 | 545 |   return nullptr; | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 546 | } | 
 | 547 | #endif | 
 | 548 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 549 | }; //  android namespace |