blob: 64847439ad705d32017f58aa8a8226d2960dc823 [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__
Dimitry Ivanovc337cae2016-05-09 17:51:51 -070022#include "dlext_namespaces.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080023#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 Ivanov0b5651e2016-04-21 16:42:48 -070040static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt";
Dimitry Ivanovb6140452016-04-06 18:24:08 -070041static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080042
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -070043// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
44// System.load() with an absolute path which is outside of the classloader library search path.
45// This list includes all directories app is allowed to access this way.
46static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
47
Dimitry Ivanov7d028292016-05-05 17:30:24 -070048static bool is_debuggable() {
49 char debuggable[PROP_VALUE_MAX];
50 property_get("ro.debuggable", debuggable, "0");
51 return std::string(debuggable) == "1";
52}
53
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080054class LibraryNamespaces {
55 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080056 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080057
Dimitry Ivanovd047c922016-02-23 14:23:51 -080058 android_namespace_t* Create(JNIEnv* env,
59 jobject class_loader,
60 bool is_shared,
61 jstring java_library_path,
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -070062 jstring java_permitted_path) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -070063 std::string library_path; // empty string by default.
64
65 if (java_library_path != nullptr) {
66 ScopedUtfChars library_path_utf_chars(env, java_library_path);
67 library_path = library_path_utf_chars.c_str();
68 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080069
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -070070 // (http://b/27588281) This is a workaround for apps using custom
71 // classloaders and calling System.load() with an absolute path which
72 // is outside of the classloader library search path.
73 //
74 // This part effectively allows such a classloader to access anything
75 // under /data and /mnt/expand
76 std::string permitted_path = kWhitelistedDirectories;
77
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080078 if (java_permitted_path != nullptr) {
79 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -070080 if (path.c_str() != nullptr && path.size() > 0) {
81 permitted_path = permitted_path + ":" + path.c_str();
82 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080083 }
84
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -070085 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080086 return nullptr;
87 }
88
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080089 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080090
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070091 LOG_ALWAYS_FATAL_IF(ns != nullptr,
92 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080093
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080094 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
95 if (is_shared) {
96 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
97 }
98
Dimitry Ivanovade364b2016-05-12 15:34:41 -070099 android_namespace_t* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
100
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -0800101 ns = android_create_namespace("classloader-namespace",
102 nullptr,
103 library_path.c_str(),
104 namespace_type,
Dimitry Ivanovade364b2016-05-12 15:34:41 -0700105 permitted_path.c_str(),
106 parent_ns);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800107
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800108 if (ns != nullptr) {
109 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
110 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800111
112 return ns;
113 }
114
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800115 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
116 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
117 [&](const std::pair<jweak, android_namespace_t*>& value) {
118 return env->IsSameObject(value.first, class_loader);
119 });
120 return it != namespaces_.end() ? it->second : nullptr;
121 }
122
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700123 void Initialize() {
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700124 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700125 const char* android_root_env = getenv("ANDROID_ROOT");
126 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
127 std::string public_native_libraries_system_config =
128 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700129
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700130 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700131 "Error reading public native library list from \"%s\": %s",
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700132 public_native_libraries_system_config.c_str(), strerror(errno));
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700133
134 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
135 // variable to add libraries to the list. This is intended for platform tests only.
136 if (is_debuggable()) {
137 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
138 if (additional_libs != nullptr && additional_libs[0] != '\0') {
139 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
140 std::copy(additional_libs_vector.begin(),
141 additional_libs_vector.end(),
142 std::back_inserter(sonames));
143 }
144 }
145
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700146 // This file is optional, quietly ignore if the file does not exist.
147 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
148
149 // android_init_namespaces() expects all the public libraries
150 // to be loaded so that they can be found by soname alone.
151 //
152 // TODO(dimitry): this is a bit misleading since we do not know
153 // if the vendor public library is going to be opened from /vendor/lib
154 // we might as well end up loading them from /system/lib
155 // For now we rely on CTS test to catch things like this but
156 // it should probably be addressed in the future.
157 for (const auto& soname : sonames) {
158 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
159 }
160
161 public_libraries_ = base::Join(sonames, ':');
162 }
163
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700164 void Reset() {
165 namespaces_.clear();
166 }
167
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700168 private:
169 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700170 // Read list of public native libraries from the config file.
171 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700172 if(!base::ReadFileToString(configFile, &file_content)) {
173 return false;
174 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700175
176 std::vector<std::string> lines = base::Split(file_content, "\n");
177
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700178 for (const auto& line : lines) {
179 auto trimmed_line = base::Trim(line);
180 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
181 continue;
182 }
183
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700184 sonames->push_back(trimmed_line);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700185 }
186
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700187 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800188 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800189
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -0700190 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700191 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
192 // code is one example) unknown to linker in which case linker uses anonymous
193 // namespace. The second argument specifies the search path for the anonymous
194 // namespace which is the library_path of the classloader.
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -0700195 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800196
197 return initialized_;
198 }
199
Dimitry Ivanovade364b2016-05-12 15:34:41 -0700200 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
201 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
202 jmethodID get_parent = env->GetMethodID(class_loader_class,
203 "getParent",
204 "()Ljava/lang/ClassLoader;");
205
206 return env->CallObjectMethod(class_loader, get_parent);
207 }
208
209 android_namespace_t* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
210 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
211
212 while (parent_class_loader != nullptr) {
213 android_namespace_t* ns = FindNamespaceByClassLoader(env, parent_class_loader);
214 if (ns != nullptr) {
215 return ns;
216 }
217
218 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
219 }
220 return nullptr;
221 }
222
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800223 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800224 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700225 std::string public_libraries_;
226
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800227
228 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
229};
230
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800231static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800232static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
233#endif
234
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700235void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800236#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800237 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700238 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800239#endif
240}
241
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700242void ResetNativeLoader() {
243#if defined(__ANDROID__)
244 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
245 g_namespaces->Reset();
246#endif
247}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800248
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800249jstring CreateClassLoaderNamespace(JNIEnv* env,
250 int32_t target_sdk_version,
251 jobject class_loader,
252 bool is_shared,
253 jstring library_path,
254 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800255#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700256 UNUSED(target_sdk_version);
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800257 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800258 android_namespace_t* ns = g_namespaces->Create(env,
259 class_loader,
260 is_shared,
261 library_path,
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -0700262 permitted_path);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800263 if (ns == nullptr) {
264 return env->NewStringUTF(dlerror());
265 }
266#else
267 UNUSED(env, target_sdk_version, class_loader, is_shared,
268 library_path, permitted_path);
269#endif
270 return nullptr;
271}
272
273void* OpenNativeLibrary(JNIEnv* env,
274 int32_t target_sdk_version,
275 const char* path,
276 jobject class_loader,
277 jstring library_path) {
278#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700279 UNUSED(target_sdk_version);
280 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800281 return dlopen(path, RTLD_NOW);
282 }
283
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800284 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800285 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800286
287 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800288 // This is the case where the classloader was not created by ApplicationLoaders
289 // In this case we create an isolated not-shared namespace for it.
Dimitry Ivanov94ee4e62016-05-24 08:37:05 -0700290 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800291 if (ns == nullptr) {
292 return nullptr;
293 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800294 }
295
296 android_dlextinfo extinfo;
297 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
298 extinfo.library_namespace = ns;
299
300 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
301#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800302 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800303 return dlopen(path, RTLD_NOW);
304#endif
305}
306
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700307bool CloseNativeLibrary(void* handle) {
308 return dlclose(handle) == 0;
309}
310
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800311#if defined(__ANDROID__)
312android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800313 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800314 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
315}
316#endif
317
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800318}; // android namespace