blob: 04143853ea652fda713b00afab4344c4b887aaa1 [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 Ivanov4cd07682016-02-23 14:23:51 -080024#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080025#endif
26
27#include <algorithm>
28#include <vector>
29#include <string>
30#include <mutex>
31
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070032#include "android-base/file.h"
Elliott Hughes8b047142015-12-08 10:38:59 -080033#include "android-base/macros.h"
34#include "android-base/strings.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080035
36namespace android {
37
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070038#if defined(__ANDROID__)
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -070039static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt";
Dimitry Ivanov617f4952016-04-06 18:24:08 -070040static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Dimitry Ivanov637da4e2016-05-10 10:39:48 -070042// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
43// System.load() with an absolute path which is outside of the classloader library search path.
44// This list includes all directories app is allowed to access this way.
45static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
46
Dimitry Ivanov4ddabd02016-05-05 17:30:24 -070047static bool is_debuggable() {
48 char debuggable[PROP_VALUE_MAX];
49 property_get("ro.debuggable", debuggable, "0");
50 return std::string(debuggable) == "1";
51}
52
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080053class LibraryNamespaces {
54 public:
Dimitry Ivanov6f800222016-02-22 11:27:48 -080055 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080056
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080057 android_namespace_t* Create(JNIEnv* env,
58 jobject class_loader,
59 bool is_shared,
60 jstring java_library_path,
61 jstring java_permitted_path) {
Dimitry Ivanov8a0425b2016-05-09 10:55:50 -070062 std::string library_path; // empty string by default.
63
64 if (java_library_path != nullptr) {
65 ScopedUtfChars library_path_utf_chars(env, java_library_path);
66 library_path = library_path_utf_chars.c_str();
67 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080068
Dimitry Ivanov637da4e2016-05-10 10:39:48 -070069 // (http://b/27588281) This is a workaround for apps using custom
70 // classloaders and calling System.load() with an absolute path which
71 // is outside of the classloader library search path.
72 //
73 // This part effectively allows such a classloader to access anything
74 // under /data and /mnt/expand
75 std::string permitted_path = kWhitelistedDirectories;
76
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080077 if (java_permitted_path != nullptr) {
78 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanov72ca40f2016-05-10 16:21:25 -070079 if (path.c_str() != nullptr && path.size() > 0) {
80 permitted_path = permitted_path + ":" + path.c_str();
81 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080082 }
83
84 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080085 return nullptr;
86 }
87
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080088 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080089
Dimitry Ivanovd1fdb982016-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 Ivanov34fa7042016-02-22 13:02:35 -080098 ns = android_create_namespace("classloader-namespace",
99 nullptr,
100 library_path.c_str(),
101 namespace_type,
102 java_permitted_path != nullptr ?
103 permitted_path.c_str() :
104 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800105
Dimitry Ivanov4cd07682016-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 Ivanov0cd10d82016-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 Ivanovd1fdb982016-03-15 13:51:26 -0700121 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700122 // Once public namespace is initialized there is no
123 // point in running this code - it will have no effect
124 // on the current list of public libraries.
125 if (initialized_) {
126 return;
127 }
128
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700129 std::vector<std::string> sonames;
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700130 const char* android_root_env = getenv("ANDROID_ROOT");
131 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
132 std::string public_native_libraries_system_config =
133 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700134
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700135 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700136 "Error reading public native library list from \"%s\": %s",
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700137 public_native_libraries_system_config.c_str(), strerror(errno));
Dimitry Ivanov4ddabd02016-05-05 17:30:24 -0700138
139 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
140 // variable to add libraries to the list. This is intended for platform tests only.
141 if (is_debuggable()) {
142 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
143 if (additional_libs != nullptr && additional_libs[0] != '\0') {
144 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
145 std::copy(additional_libs_vector.begin(),
146 additional_libs_vector.end(),
147 std::back_inserter(sonames));
148 }
149 }
150
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700151 // This file is optional, quietly ignore if the file does not exist.
152 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
153
154 // android_init_namespaces() expects all the public libraries
155 // to be loaded so that they can be found by soname alone.
156 //
157 // TODO(dimitry): this is a bit misleading since we do not know
158 // if the vendor public library is going to be opened from /vendor/lib
159 // we might as well end up loading them from /system/lib
160 // For now we rely on CTS test to catch things like this but
161 // it should probably be addressed in the future.
162 for (const auto& soname : sonames) {
163 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
164 }
165
166 public_libraries_ = base::Join(sonames, ':');
167 }
168
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700169 void Reset() {
170 namespaces_.clear();
171 }
172
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700173 private:
174 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700175 // Read list of public native libraries from the config file.
176 std::string file_content;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700177 if(!base::ReadFileToString(configFile, &file_content)) {
178 return false;
179 }
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700180
181 std::vector<std::string> lines = base::Split(file_content, "\n");
182
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700183 for (const auto& line : lines) {
184 auto trimmed_line = base::Trim(line);
185 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
186 continue;
187 }
188
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700189 sonames->push_back(trimmed_line);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700190 }
191
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700192 return true;
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800193 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800194
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800195 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700196 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
197 // code is one example) unknown to linker in which case linker uses anonymous
198 // namespace. The second argument specifies the search path for the anonymous
199 // namespace which is the library_path of the classloader.
200 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800201
202 return initialized_;
203 }
204
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800205 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800206 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700207 std::string public_libraries_;
208
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800209
210 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
211};
212
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800213static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800214static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
215#endif
216
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700217void InitializeNativeLoader() {
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800218#if defined(__ANDROID__)
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800219 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700220 g_namespaces->Initialize();
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800221#endif
222}
223
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700224void ResetNativeLoader() {
225#if defined(__ANDROID__)
226 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
227 g_namespaces->Reset();
228#endif
229}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800230
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800231jstring CreateClassLoaderNamespace(JNIEnv* env,
232 int32_t target_sdk_version,
233 jobject class_loader,
234 bool is_shared,
235 jstring library_path,
236 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800237#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700238 UNUSED(target_sdk_version);
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800239 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800240 android_namespace_t* ns = g_namespaces->Create(env,
241 class_loader,
242 is_shared,
243 library_path,
244 permitted_path);
245 if (ns == nullptr) {
246 return env->NewStringUTF(dlerror());
247 }
248#else
249 UNUSED(env, target_sdk_version, class_loader, is_shared,
250 library_path, permitted_path);
251#endif
252 return nullptr;
253}
254
255void* OpenNativeLibrary(JNIEnv* env,
256 int32_t target_sdk_version,
257 const char* path,
258 jobject class_loader,
259 jstring library_path) {
260#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700261 UNUSED(target_sdk_version);
262 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800263 return dlopen(path, RTLD_NOW);
264 }
265
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800266 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800267 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800268
269 if (ns == nullptr) {
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800270 // This is the case where the classloader was not created by ApplicationLoaders
271 // In this case we create an isolated not-shared namespace for it.
272 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
273 if (ns == nullptr) {
274 return nullptr;
275 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800276 }
277
278 android_dlextinfo extinfo;
279 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
280 extinfo.library_namespace = ns;
281
282 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
283#else
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800284 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800285 return dlopen(path, RTLD_NOW);
286#endif
287}
288
Dimitry Ivanov7a1f9512016-05-03 14:55:25 -0700289bool CloseNativeLibrary(void* handle) {
290 return dlclose(handle) == 0;
291}
292
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800293#if defined(__ANDROID__)
294android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800295 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800296 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
297}
298#endif
299
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800300}; // android namespace