blob: 0b49b4106095d60189f3f276e205ac09edfdf85c [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 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,
62 jstring java_permitted_path,
63 int32_t target_sdk_version) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -070064 std::string library_path; // empty string by default.
65
66 if (java_library_path != nullptr) {
67 ScopedUtfChars library_path_utf_chars(env, java_library_path);
68 library_path = library_path_utf_chars.c_str();
69 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080070
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -070071 // (http://b/27588281) This is a workaround for apps using custom
72 // classloaders and calling System.load() with an absolute path which
73 // is outside of the classloader library search path.
74 //
75 // This part effectively allows such a classloader to access anything
76 // under /data and /mnt/expand
77 std::string permitted_path = kWhitelistedDirectories;
78
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080079 if (java_permitted_path != nullptr) {
80 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -070081 permitted_path = permitted_path + ":" + path.c_str();
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080082 }
83
Dimitry Ivanovdab56732016-02-17 17:20:15 -080084 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080085 return nullptr;
86 }
87
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080088 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080089
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070090 LOG_ALWAYS_FATAL_IF(ns != nullptr,
91 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080092
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080093 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
94 if (is_shared) {
95 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
96 }
97
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080098 ns = android_create_namespace("classloader-namespace",
99 nullptr,
100 library_path.c_str(),
101 namespace_type,
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -0700102 !permitted_path.empty() ?
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -0800103 permitted_path.c_str() :
104 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800105
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800106 if (ns != nullptr) {
107 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
108 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800109
110 return ns;
111 }
112
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800113 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
114 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
115 [&](const std::pair<jweak, android_namespace_t*>& value) {
116 return env->IsSameObject(value.first, class_loader);
117 });
118 return it != namespaces_.end() ? it->second : nullptr;
119 }
120
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700121 void Initialize() {
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700122 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700123 const char* android_root_env = getenv("ANDROID_ROOT");
124 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
125 std::string public_native_libraries_system_config =
126 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700127
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700128 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700129 "Error reading public native library list from \"%s\": %s",
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700130 public_native_libraries_system_config.c_str(), strerror(errno));
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700131
132 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
133 // variable to add libraries to the list. This is intended for platform tests only.
134 if (is_debuggable()) {
135 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
136 if (additional_libs != nullptr && additional_libs[0] != '\0') {
137 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
138 std::copy(additional_libs_vector.begin(),
139 additional_libs_vector.end(),
140 std::back_inserter(sonames));
141 }
142 }
143
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700144 // This file is optional, quietly ignore if the file does not exist.
145 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
146
147 // android_init_namespaces() expects all the public libraries
148 // to be loaded so that they can be found by soname alone.
149 //
150 // TODO(dimitry): this is a bit misleading since we do not know
151 // if the vendor public library is going to be opened from /vendor/lib
152 // we might as well end up loading them from /system/lib
153 // For now we rely on CTS test to catch things like this but
154 // it should probably be addressed in the future.
155 for (const auto& soname : sonames) {
156 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
157 }
158
159 public_libraries_ = base::Join(sonames, ':');
160 }
161
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700162 void Reset() {
163 namespaces_.clear();
164 }
165
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700166 private:
167 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700168 // Read list of public native libraries from the config file.
169 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700170 if(!base::ReadFileToString(configFile, &file_content)) {
171 return false;
172 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700173
174 std::vector<std::string> lines = base::Split(file_content, "\n");
175
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700176 for (const auto& line : lines) {
177 auto trimmed_line = base::Trim(line);
178 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
179 continue;
180 }
181
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700182 sonames->push_back(trimmed_line);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700183 }
184
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700185 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800186 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800187
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800188 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700189 std::string publicNativeLibraries = public_libraries_;
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800190
Alex Light3150fa22016-04-15 10:18:43 -0700191 UNUSED(target_sdk_version);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700192 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
193 // code is one example) unknown to linker in which case linker uses anonymous
194 // namespace. The second argument specifies the search path for the anonymous
195 // namespace which is the library_path of the classloader.
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800196 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800197
198 return initialized_;
199 }
200
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800201 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800202 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700203 std::string public_libraries_;
204
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800205
206 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
207};
208
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800209static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800210static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
211#endif
212
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700213void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800214#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800215 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700216 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800217#endif
218}
219
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700220void ResetNativeLoader() {
221#if defined(__ANDROID__)
222 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
223 g_namespaces->Reset();
224#endif
225}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800226
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800227jstring CreateClassLoaderNamespace(JNIEnv* env,
228 int32_t target_sdk_version,
229 jobject class_loader,
230 bool is_shared,
231 jstring library_path,
232 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800233#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700234 UNUSED(target_sdk_version);
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800235 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800236 android_namespace_t* ns = g_namespaces->Create(env,
237 class_loader,
238 is_shared,
239 library_path,
240 permitted_path,
241 target_sdk_version);
242 if (ns == nullptr) {
243 return env->NewStringUTF(dlerror());
244 }
245#else
246 UNUSED(env, target_sdk_version, class_loader, is_shared,
247 library_path, permitted_path);
248#endif
249 return nullptr;
250}
251
252void* OpenNativeLibrary(JNIEnv* env,
253 int32_t target_sdk_version,
254 const char* path,
255 jobject class_loader,
256 jstring library_path) {
257#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700258 UNUSED(target_sdk_version);
259 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800260 return dlopen(path, RTLD_NOW);
261 }
262
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800263 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800264 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800265
266 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800267 // This is the case where the classloader was not created by ApplicationLoaders
268 // In this case we create an isolated not-shared namespace for it.
269 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version);
270 if (ns == nullptr) {
271 return nullptr;
272 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800273 }
274
275 android_dlextinfo extinfo;
276 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
277 extinfo.library_namespace = ns;
278
279 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
280#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800281 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800282 return dlopen(path, RTLD_NOW);
283#endif
284}
285
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700286bool CloseNativeLibrary(void* handle) {
287 return dlclose(handle) == 0;
288}
289
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800290#if defined(__ANDROID__)
291android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800292 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800293 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
294}
295#endif
296
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800297}; // android namespace