blob: 81dc3166768d89511b912fb2ea5abad61732502e [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
43class LibraryNamespaces {
44 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080045 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080046
Dimitry Ivanovd047c922016-02-23 14:23:51 -080047 android_namespace_t* Create(JNIEnv* env,
48 jobject class_loader,
49 bool is_shared,
50 jstring java_library_path,
51 jstring java_permitted_path,
52 int32_t target_sdk_version) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080053 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080054
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080055 std::string permitted_path;
56 if (java_permitted_path != nullptr) {
57 ScopedUtfChars path(env, java_permitted_path);
58 permitted_path = path.c_str();
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070059 } else {
60 // (http://b/27588281) This is a workaround for apps using custom
61 // classloaders and calling System.load() with an absolute path which
62 // is outside of the classloader library search path.
63 //
64 // This part effectively allows such a classloader to access anything
65 // under /data
66 permitted_path = "/data";
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080067 }
68
Dimitry Ivanovdab56732016-02-17 17:20:15 -080069 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080070 return nullptr;
71 }
72
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080073 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080074
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070075 LOG_ALWAYS_FATAL_IF(ns != nullptr,
76 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080077
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080078 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
79 if (is_shared) {
80 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
81 }
82
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080083 ns = android_create_namespace("classloader-namespace",
84 nullptr,
85 library_path.c_str(),
86 namespace_type,
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070087 !permitted_path.empty() ?
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080088 permitted_path.c_str() :
89 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080090
Dimitry Ivanovd047c922016-02-23 14:23:51 -080091 if (ns != nullptr) {
92 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
93 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080094
95 return ns;
96 }
97
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -080098 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
99 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
100 [&](const std::pair<jweak, android_namespace_t*>& value) {
101 return env->IsSameObject(value.first, class_loader);
102 });
103 return it != namespaces_.end() ? it->second : nullptr;
104 }
105
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700106 void Initialize() {
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700107 std::vector<std::string> sonames;
108
109 LOG_ALWAYS_FATAL_IF(!ReadConfig(kPublicNativeLibrariesSystemConfig, &sonames),
110 "Error reading public native library list from \"%s\": %s",
111 kPublicNativeLibrariesSystemConfig, strerror(errno));
112 // This file is optional, quietly ignore if the file does not exist.
113 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
114
115 // android_init_namespaces() expects all the public libraries
116 // to be loaded so that they can be found by soname alone.
117 //
118 // TODO(dimitry): this is a bit misleading since we do not know
119 // if the vendor public library is going to be opened from /vendor/lib
120 // we might as well end up loading them from /system/lib
121 // For now we rely on CTS test to catch things like this but
122 // it should probably be addressed in the future.
123 for (const auto& soname : sonames) {
124 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
125 }
126
127 public_libraries_ = base::Join(sonames, ':');
128 }
129
130 private:
131 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700132 // Read list of public native libraries from the config file.
133 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700134 if(!base::ReadFileToString(configFile, &file_content)) {
135 return false;
136 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700137
138 std::vector<std::string> lines = base::Split(file_content, "\n");
139
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700140 for (const auto& line : lines) {
141 auto trimmed_line = base::Trim(line);
142 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
143 continue;
144 }
145
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700146 sonames->push_back(trimmed_line);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700147 }
148
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700149 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800150 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800151
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800152 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700153 std::string publicNativeLibraries = public_libraries_;
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800154
Alex Light3150fa22016-04-15 10:18:43 -0700155 UNUSED(target_sdk_version);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700156 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
157 // code is one example) unknown to linker in which case linker uses anonymous
158 // namespace. The second argument specifies the search path for the anonymous
159 // namespace which is the library_path of the classloader.
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800160 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800161
162 return initialized_;
163 }
164
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800165 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800166 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700167 std::string public_libraries_;
168
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800169
170 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
171};
172
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800173static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800174static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800175
176static bool namespaces_enabled(uint32_t target_sdk_version) {
177 return target_sdk_version > 0;
178}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800179#endif
180
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700181void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800182#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800183 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700184 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800185#endif
186}
187
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800188
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800189jstring CreateClassLoaderNamespace(JNIEnv* env,
190 int32_t target_sdk_version,
191 jobject class_loader,
192 bool is_shared,
193 jstring library_path,
194 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800195#if defined(__ANDROID__)
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800196 if (!namespaces_enabled(target_sdk_version)) {
197 return nullptr;
198 }
199
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800200 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800201 android_namespace_t* ns = g_namespaces->Create(env,
202 class_loader,
203 is_shared,
204 library_path,
205 permitted_path,
206 target_sdk_version);
207 if (ns == nullptr) {
208 return env->NewStringUTF(dlerror());
209 }
210#else
211 UNUSED(env, target_sdk_version, class_loader, is_shared,
212 library_path, permitted_path);
213#endif
214 return nullptr;
215}
216
217void* OpenNativeLibrary(JNIEnv* env,
218 int32_t target_sdk_version,
219 const char* path,
220 jobject class_loader,
221 jstring library_path) {
222#if defined(__ANDROID__)
223 if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800224 return dlopen(path, RTLD_NOW);
225 }
226
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800227 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800228 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800229
230 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800231 // This is the case where the classloader was not created by ApplicationLoaders
232 // In this case we create an isolated not-shared namespace for it.
233 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version);
234 if (ns == nullptr) {
235 return nullptr;
236 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800237 }
238
239 android_dlextinfo extinfo;
240 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
241 extinfo.library_namespace = ns;
242
243 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
244#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800245 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800246 return dlopen(path, RTLD_NOW);
247#endif
248}
249
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800250#if defined(__ANDROID__)
251android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800252 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800253 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
254}
255#endif
256
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800257}; // android namespace