blob: dca316c56f9c96cbb8b7e8912a44babaacb4875b [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 Ivanov637da4e2016-05-10 10:39:48 -070079 permitted_path = permitted_path + ":" + path.c_str();
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080080 }
81
82 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080083 return nullptr;
84 }
85
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080086 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080087
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070088 LOG_ALWAYS_FATAL_IF(ns != nullptr,
89 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080090
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080091 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
92 if (is_shared) {
93 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
94 }
95
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080096 ns = android_create_namespace("classloader-namespace",
97 nullptr,
98 library_path.c_str(),
99 namespace_type,
100 java_permitted_path != nullptr ?
101 permitted_path.c_str() :
102 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800103
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800104 if (ns != nullptr) {
105 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
106 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800107
108 return ns;
109 }
110
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800111 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
112 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
113 [&](const std::pair<jweak, android_namespace_t*>& value) {
114 return env->IsSameObject(value.first, class_loader);
115 });
116 return it != namespaces_.end() ? it->second : nullptr;
117 }
118
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700119 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700120 // Once public namespace is initialized there is no
121 // point in running this code - it will have no effect
122 // on the current list of public libraries.
123 if (initialized_) {
124 return;
125 }
126
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700127 std::vector<std::string> sonames;
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700128 const char* android_root_env = getenv("ANDROID_ROOT");
129 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
130 std::string public_native_libraries_system_config =
131 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700132
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700133 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700134 "Error reading public native library list from \"%s\": %s",
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700135 public_native_libraries_system_config.c_str(), strerror(errno));
Dimitry Ivanov4ddabd02016-05-05 17:30:24 -0700136
137 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
138 // variable to add libraries to the list. This is intended for platform tests only.
139 if (is_debuggable()) {
140 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
141 if (additional_libs != nullptr && additional_libs[0] != '\0') {
142 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
143 std::copy(additional_libs_vector.begin(),
144 additional_libs_vector.end(),
145 std::back_inserter(sonames));
146 }
147 }
148
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700149 // This file is optional, quietly ignore if the file does not exist.
150 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
151
152 // android_init_namespaces() expects all the public libraries
153 // to be loaded so that they can be found by soname alone.
154 //
155 // TODO(dimitry): this is a bit misleading since we do not know
156 // if the vendor public library is going to be opened from /vendor/lib
157 // we might as well end up loading them from /system/lib
158 // For now we rely on CTS test to catch things like this but
159 // it should probably be addressed in the future.
160 for (const auto& soname : sonames) {
161 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
162 }
163
164 public_libraries_ = base::Join(sonames, ':');
165 }
166
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700167 void Reset() {
168 namespaces_.clear();
169 }
170
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700171 private:
172 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700173 // Read list of public native libraries from the config file.
174 std::string file_content;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700175 if(!base::ReadFileToString(configFile, &file_content)) {
176 return false;
177 }
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700178
179 std::vector<std::string> lines = base::Split(file_content, "\n");
180
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700181 for (const auto& line : lines) {
182 auto trimmed_line = base::Trim(line);
183 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
184 continue;
185 }
186
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700187 sonames->push_back(trimmed_line);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700188 }
189
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700190 return true;
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800191 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800192
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800193 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700194 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
195 // code is one example) unknown to linker in which case linker uses anonymous
196 // namespace. The second argument specifies the search path for the anonymous
197 // namespace which is the library_path of the classloader.
198 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800199
200 return initialized_;
201 }
202
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800203 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800204 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700205 std::string public_libraries_;
206
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800207
208 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
209};
210
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800211static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800212static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
213#endif
214
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700215void InitializeNativeLoader() {
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800216#if defined(__ANDROID__)
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800217 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700218 g_namespaces->Initialize();
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800219#endif
220}
221
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700222void ResetNativeLoader() {
223#if defined(__ANDROID__)
224 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
225 g_namespaces->Reset();
226#endif
227}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800228
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800229jstring CreateClassLoaderNamespace(JNIEnv* env,
230 int32_t target_sdk_version,
231 jobject class_loader,
232 bool is_shared,
233 jstring library_path,
234 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800235#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700236 UNUSED(target_sdk_version);
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800237 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800238 android_namespace_t* ns = g_namespaces->Create(env,
239 class_loader,
240 is_shared,
241 library_path,
242 permitted_path);
243 if (ns == nullptr) {
244 return env->NewStringUTF(dlerror());
245 }
246#else
247 UNUSED(env, target_sdk_version, class_loader, is_shared,
248 library_path, permitted_path);
249#endif
250 return nullptr;
251}
252
253void* OpenNativeLibrary(JNIEnv* env,
254 int32_t target_sdk_version,
255 const char* path,
256 jobject class_loader,
257 jstring library_path) {
258#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700259 UNUSED(target_sdk_version);
260 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800261 return dlopen(path, RTLD_NOW);
262 }
263
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800264 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800265 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800266
267 if (ns == nullptr) {
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800268 // This is the case where the classloader was not created by ApplicationLoaders
269 // In this case we create an isolated not-shared namespace for it.
270 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
271 if (ns == nullptr) {
272 return nullptr;
273 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800274 }
275
276 android_dlextinfo extinfo;
277 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
278 extinfo.library_namespace = ns;
279
280 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
281#else
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800282 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800283 return dlopen(path, RTLD_NOW);
284#endif
285}
286
Dimitry Ivanov7a1f9512016-05-03 14:55:25 -0700287bool CloseNativeLibrary(void* handle) {
288 return dlclose(handle) == 0;
289}
290
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800291#if defined(__ANDROID__)
292android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800293 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800294 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
295}
296#endif
297
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800298}; // android namespace