blob: ec7a941496b7b3ca08d69e03a77857e417b1964e [file] [log] [blame]
Dimitry Ivanovac1b1912015-12-01 13:56:44 -08001/*
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 Ivanov7f9a1aa2016-03-22 13:59:59 -070024#define LOG_TAG "libnativeloader"
Dimitry Ivanovd047c922016-02-23 14:23:51 -080025#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080026#endif
27
28#include <algorithm>
29#include <vector>
30#include <string>
31#include <mutex>
32
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070033#include "android-base/file.h"
Elliott Hughes8b047142015-12-08 10:38:59 -080034#include "android-base/macros.h"
35#include "android-base/strings.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080036
37namespace android {
38
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070039#if defined(__ANDROID__)
40static constexpr const char* kPublicNativeLibrariesConfig = "/system/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070042static bool namespace_workaround_enabled(int32_t target_sdk_version) {
Dimitry Ivanov4f8bb252016-03-29 16:10:02 -070043 return target_sdk_version <= 23;
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070044}
45
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080046class LibraryNamespaces {
47 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080048 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080049
Dimitry Ivanovd047c922016-02-23 14:23:51 -080050 android_namespace_t* Create(JNIEnv* env,
51 jobject class_loader,
52 bool is_shared,
53 jstring java_library_path,
54 jstring java_permitted_path,
55 int32_t target_sdk_version) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080056 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080057
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080058 std::string permitted_path;
59 if (java_permitted_path != nullptr) {
60 ScopedUtfChars path(env, java_permitted_path);
61 permitted_path = path.c_str();
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070062 } else {
63 // (http://b/27588281) This is a workaround for apps using custom
64 // classloaders and calling System.load() with an absolute path which
65 // is outside of the classloader library search path.
66 //
67 // This part effectively allows such a classloader to access anything
68 // under /data
69 permitted_path = "/data";
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080070 }
71
Dimitry Ivanovdab56732016-02-17 17:20:15 -080072 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080073 return nullptr;
74 }
75
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080076 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080077
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070078 LOG_ALWAYS_FATAL_IF(ns != nullptr,
79 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080080
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080081 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
82 if (is_shared) {
83 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
84 }
85
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080086 ns = android_create_namespace("classloader-namespace",
87 nullptr,
88 library_path.c_str(),
89 namespace_type,
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070090 !permitted_path.empty() ?
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080091 permitted_path.c_str() :
92 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080093
Dimitry Ivanovd047c922016-02-23 14:23:51 -080094 if (ns != nullptr) {
95 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
96 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080097
98 return ns;
99 }
100
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800101 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
102 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
103 [&](const std::pair<jweak, android_namespace_t*>& value) {
104 return env->IsSameObject(value.first, class_loader);
105 });
106 return it != namespaces_.end() ? it->second : nullptr;
107 }
108
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700109 void Initialize() {
110 // Read list of public native libraries from the config file.
111 std::string file_content;
112 LOG_ALWAYS_FATAL_IF(!base::ReadFileToString(kPublicNativeLibrariesConfig, &file_content),
113 "Error reading public native library list from \"%s\": %s",
114 kPublicNativeLibrariesConfig, strerror(errno));
115
116 std::vector<std::string> lines = base::Split(file_content, "\n");
117
118 std::vector<std::string> sonames;
119
120 for (const auto& line : lines) {
121 auto trimmed_line = base::Trim(line);
122 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
123 continue;
124 }
125
126 sonames.push_back(trimmed_line);
127 }
128
129 public_libraries_ = base::Join(sonames, ':');
130
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800131 // android_init_namespaces() expects all the public libraries
132 // to be loaded so that they can be found by soname alone.
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800133 for (const auto& soname : sonames) {
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800134 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800135 }
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800136 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800137
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800138 private:
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800139 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700140 std::string publicNativeLibraries = public_libraries_;
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800141
142 // TODO (dimitry): This is a workaround for http://b/26436837
143 // will be removed before the release.
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -0700144 if (namespace_workaround_enabled(target_sdk_version)) {
Dimitry Ivanov5f28b842016-03-04 11:00:37 -0800145 // check if libart.so is loaded.
146 void* handle = dlopen("libart.so", RTLD_NOW | RTLD_NOLOAD);
147 if (handle != nullptr) {
148 publicNativeLibraries += ":libart.so";
149 dlclose(handle);
150 }
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800151 }
152 // END OF WORKAROUND
153
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700154 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
155 // code is one example) unknown to linker in which case linker uses anonymous
156 // namespace. The second argument specifies the search path for the anonymous
157 // namespace which is the library_path of the classloader.
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800158 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800159
160 return initialized_;
161 }
162
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800163 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800164 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700165 std::string public_libraries_;
166
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800167
168 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
169};
170
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800171static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800172static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800173
174static bool namespaces_enabled(uint32_t target_sdk_version) {
175 return target_sdk_version > 0;
176}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800177#endif
178
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700179void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800180#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800181 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700182 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800183#endif
184}
185
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800186
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800187jstring CreateClassLoaderNamespace(JNIEnv* env,
188 int32_t target_sdk_version,
189 jobject class_loader,
190 bool is_shared,
191 jstring library_path,
192 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800193#if defined(__ANDROID__)
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800194 if (!namespaces_enabled(target_sdk_version)) {
195 return nullptr;
196 }
197
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800198 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800199 android_namespace_t* ns = g_namespaces->Create(env,
200 class_loader,
201 is_shared,
202 library_path,
203 permitted_path,
204 target_sdk_version);
205 if (ns == nullptr) {
206 return env->NewStringUTF(dlerror());
207 }
208#else
209 UNUSED(env, target_sdk_version, class_loader, is_shared,
210 library_path, permitted_path);
211#endif
212 return nullptr;
213}
214
215void* OpenNativeLibrary(JNIEnv* env,
216 int32_t target_sdk_version,
217 const char* path,
218 jobject class_loader,
219 jstring library_path) {
220#if defined(__ANDROID__)
221 if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800222 return dlopen(path, RTLD_NOW);
223 }
224
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800225 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800226 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800227
228 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800229 // This is the case where the classloader was not created by ApplicationLoaders
230 // In this case we create an isolated not-shared namespace for it.
231 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version);
232 if (ns == nullptr) {
233 return nullptr;
234 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800235 }
236
237 android_dlextinfo extinfo;
238 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
239 extinfo.library_namespace = ns;
240
241 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
242#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800243 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800244 return dlopen(path, RTLD_NOW);
245#endif
246}
247
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800248#if defined(__ANDROID__)
249android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800250 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800251 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
252}
253#endif
254
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800255}; // android namespace