blob: f28a693887e73791e16e1de3fce4616c1e5546f6 [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__)
Dimitry Ivanovb6140452016-04-06 18:24:08 -070040static constexpr const char* kPublicNativeLibrariesSystemConfig = "/system/etc/public.libraries.txt";
41static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080042
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070043static bool namespace_workaround_enabled(int32_t target_sdk_version) {
Dimitry Ivanov4f8bb252016-03-29 16:10:02 -070044 return target_sdk_version <= 23;
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070045}
46
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080047class LibraryNamespaces {
48 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080049 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080050
Dimitry Ivanovd047c922016-02-23 14:23:51 -080051 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 Ivanov0d6e5942015-12-08 11:16:56 -080057 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080058
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080059 std::string permitted_path;
60 if (java_permitted_path != nullptr) {
61 ScopedUtfChars path(env, java_permitted_path);
62 permitted_path = path.c_str();
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070063 } 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 Ivanov0d6e5942015-12-08 11:16:56 -080071 }
72
Dimitry Ivanovdab56732016-02-17 17:20:15 -080073 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080074 return nullptr;
75 }
76
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080077 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080078
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070079 LOG_ALWAYS_FATAL_IF(ns != nullptr,
80 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080081
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080082 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
83 if (is_shared) {
84 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
85 }
86
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080087 ns = android_create_namespace("classloader-namespace",
88 nullptr,
89 library_path.c_str(),
90 namespace_type,
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070091 !permitted_path.empty() ?
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080092 permitted_path.c_str() :
93 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080094
Dimitry Ivanovd047c922016-02-23 14:23:51 -080095 if (ns != nullptr) {
96 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
97 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080098
99 return ns;
100 }
101
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800102 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 Ivanov4b0e9632016-03-15 13:51:26 -0700110 void Initialize() {
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700111 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 Ivanov4b0e9632016-03-15 13:51:26 -0700136 // Read list of public native libraries from the config file.
137 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700138 if(!base::ReadFileToString(configFile, &file_content)) {
139 return false;
140 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700141
142 std::vector<std::string> lines = base::Split(file_content, "\n");
143
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700144 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 Ivanovb6140452016-04-06 18:24:08 -0700150 sonames->push_back(trimmed_line);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700151 }
152
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700153 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800154 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800155
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800156 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700157 std::string publicNativeLibraries = public_libraries_;
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800158
159 // TODO (dimitry): This is a workaround for http://b/26436837
160 // will be removed before the release.
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -0700161 if (namespace_workaround_enabled(target_sdk_version)) {
Dimitry Ivanov5f28b842016-03-04 11:00:37 -0800162 // 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 Ivanovdab56732016-02-17 17:20:15 -0800168 }
169 // END OF WORKAROUND
170
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700171 // (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 Ivanovdab56732016-02-17 17:20:15 -0800175 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800176
177 return initialized_;
178 }
179
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800180 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800181 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700182 std::string public_libraries_;
183
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800184
185 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
186};
187
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800188static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800189static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800190
191static bool namespaces_enabled(uint32_t target_sdk_version) {
192 return target_sdk_version > 0;
193}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800194#endif
195
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700196void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800197#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800198 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700199 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800200#endif
201}
202
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800203
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800204jstring 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 Ivanovac1b1912015-12-01 13:56:44 -0800210#if defined(__ANDROID__)
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800211 if (!namespaces_enabled(target_sdk_version)) {
212 return nullptr;
213 }
214
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800215 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800216 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
232void* 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 Ivanovac1b1912015-12-01 13:56:44 -0800239 return dlopen(path, RTLD_NOW);
240 }
241
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800242 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800243 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800244
245 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800246 // 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 Ivanovac1b1912015-12-01 13:56:44 -0800252 }
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 Ivanovd047c922016-02-23 14:23:51 -0800260 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800261 return dlopen(path, RTLD_NOW);
262#endif
263}
264
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800265#if defined(__ANDROID__)
266android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800267 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800268 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
269}
270#endif
271
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800272}; // android namespace