blob: de7ea0885d16e2e2615dd07022dc22b7fc339188 [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"
Steven Moreland00fe3ad2017-07-18 16:53:54 -070018#include <nativehelper/ScopedUtfChars.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080019
20#include <dlfcn.h>
21#ifdef __ANDROID__
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070022#define LOG_TAG "libnativeloader"
Jesse Hallb75d82b2017-01-09 16:04:28 -080023#include "nativeloader/dlext_namespaces.h"
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024#include "cutils/properties.h"
Mark Salyzyn30f991f2017-01-10 13:19:54 -080025#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080026#endif
Jiyong Parkd1006fe2017-11-08 18:44:09 +090027#include <dirent.h>
28#include <sys/types.h>
Zhenhua WANGf2804e52016-05-30 11:16:08 +080029#include "nativebridge/native_bridge.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080030
31#include <algorithm>
Victor Khimenko1443ec42018-07-09 17:01:22 +020032#include <list>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090033#include <memory>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080034#include <mutex>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090035#include <string>
36#include <vector>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080037
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include <android-base/file.h>
39#include <android-base/macros.h>
40#include <android-base/strings.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Justin Yun4a1d1102017-11-27 17:04:14 +090042#ifdef __BIONIC__
43#include <android-base/properties.h>
44#endif
45
Zhenhua WANGf2804e52016-05-30 11:16:08 +080046#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
47 "%s:%d: %s CHECK '" #predicate "' failed.",\
48 __FILE__, __LINE__, __FUNCTION__)
49
Inseob Kim67cb0562018-05-04 11:39:12 +090050using namespace std::string_literals;
51
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080052namespace android {
53
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070054#if defined(__ANDROID__)
Zhenhua WANGf2804e52016-05-30 11:16:08 +080055class NativeLoaderNamespace {
56 public:
57 NativeLoaderNamespace()
58 : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
59
60 explicit NativeLoaderNamespace(android_namespace_t* ns)
61 : android_ns_(ns), native_bridge_ns_(nullptr) { }
62
63 explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
64 : android_ns_(nullptr), native_bridge_ns_(ns) { }
65
66 NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
67 NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
68
69 NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
70
71 android_namespace_t* get_android_ns() const {
72 CHECK(native_bridge_ns_ == nullptr);
73 return android_ns_;
74 }
75
76 native_bridge_namespace_t* get_native_bridge_ns() const {
77 CHECK(android_ns_ == nullptr);
78 return native_bridge_ns_;
79 }
80
81 bool is_android_namespace() const {
82 return native_bridge_ns_ == nullptr;
83 }
84
85 private:
86 // Only one of them can be not null
87 android_namespace_t* android_ns_;
88 native_bridge_namespace_t* native_bridge_ns_;
89};
90
Jiyong Parkd1006fe2017-11-08 18:44:09 +090091static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] =
92 "/etc/public.libraries.txt";
93static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
94static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
95 sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
96static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
97static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
98 sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
99static constexpr const char kPublicNativeLibrariesVendorConfig[] =
100 "/vendor/etc/public.libraries.txt";
101static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] =
102 "/etc/llndk.libraries.txt";
103static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] =
104 "/etc/vndksp.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800105
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700106// The device may be configured to have the vendor libraries loaded to a separate namespace.
107// For historical reasons this namespace was named sphal but effectively it is intended
108// to use to load vendor libraries to separate namespace with controlled interface between
109// vendor and system namespaces.
110static constexpr const char* kVendorNamespaceName = "sphal";
111
Jiyong Parka07f3052017-08-22 10:26:10 +0900112static constexpr const char* kVndkNamespaceName = "vndk";
113
114static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
115static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
116
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700117// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
118// System.load() with an absolute path which is outside of the classloader library search path.
119// This list includes all directories app is allowed to access this way.
120static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
121
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700122static bool is_debuggable() {
123 char debuggable[PROP_VALUE_MAX];
124 property_get("ro.debuggable", debuggable, "0");
125 return std::string(debuggable) == "1";
126}
127
Justin Yun4a1d1102017-11-27 17:04:14 +0900128static std::string vndk_version_str() {
129#ifdef __BIONIC__
130 std::string version = android::base::GetProperty("ro.vndk.version", "");
131 if (version != "" && version != "current") {
132 return "." + version;
133 }
134#endif
135 return "";
136}
137
138static void insert_vndk_version_str(std::string* file_name) {
139 CHECK(file_name != nullptr);
140 size_t insert_pos = file_name->find_last_of(".");
141 if (insert_pos == std::string::npos) {
142 insert_pos = file_name->length();
143 }
144 file_name->insert(insert_pos, vndk_version_str());
145}
146
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900147static const std::function<bool(const std::string&, std::string*)> always_true =
148 [](const std::string&, std::string*) { return true; };
149
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800150class LibraryNamespaces {
151 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800152 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800153
Victor Khimenko1443ec42018-07-09 17:01:22 +0200154 NativeLoaderNamespace* Create(JNIEnv* env,
155 uint32_t target_sdk_version,
156 jobject class_loader,
157 bool is_shared,
158 bool is_for_vendor,
159 jstring java_library_path,
160 jstring java_permitted_path,
161 std::string* error_msg) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -0700162 std::string library_path; // empty string by default.
163
164 if (java_library_path != nullptr) {
165 ScopedUtfChars library_path_utf_chars(env, java_library_path);
166 library_path = library_path_utf_chars.c_str();
167 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800168
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700169 // (http://b/27588281) This is a workaround for apps using custom
170 // classloaders and calling System.load() with an absolute path which
171 // is outside of the classloader library search path.
172 //
173 // This part effectively allows such a classloader to access anything
174 // under /data and /mnt/expand
175 std::string permitted_path = kWhitelistedDirectories;
176
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800177 if (java_permitted_path != nullptr) {
178 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -0700179 if (path.c_str() != nullptr && path.size() > 0) {
180 permitted_path = permitted_path + ":" + path.c_str();
181 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800182 }
183
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800184 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200185 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800186 }
187
Victor Khimenko1443ec42018-07-09 17:01:22 +0200188 bool found = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800189
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800190 LOG_ALWAYS_FATAL_IF(found,
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700191 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800192
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800193 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
194 if (is_shared) {
195 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
196 }
197
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700198 if (target_sdk_version < 24) {
199 namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
200 }
201
Victor Khimenko1443ec42018-07-09 17:01:22 +0200202 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700203
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800204 bool is_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800205
Victor Khimenko1443ec42018-07-09 17:01:22 +0200206 if (parent_ns != nullptr) {
207 is_native_bridge = !parent_ns->is_android_namespace();
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800208 } else if (!library_path.empty()) {
209 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800210 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800211
Jiyong Parka07f3052017-08-22 10:26:10 +0900212 std::string system_exposed_libraries = system_public_libraries_;
213 const char* namespace_name = kClassloaderNamespaceName;
214 android_namespace_t* vndk_ns = nullptr;
215 if (is_for_vendor && !is_shared) {
216 LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture");
217
218 // For vendor apks, give access to the vendor lib even though
219 // they are treated as unbundled; the libs and apks are still bundled
220 // together in the vendor partition.
221#if defined(__LP64__)
222 std::string vendor_lib_path = "/vendor/lib64";
223#else
224 std::string vendor_lib_path = "/vendor/lib";
225#endif
226 library_path = library_path + ":" + vendor_lib_path.c_str();
227 permitted_path = permitted_path + ":" + vendor_lib_path.c_str();
228
229 // Also give access to LLNDK libraries since they are available to vendors
230 system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str();
231
232 // Give access to VNDK-SP libraries from the 'vndk' namespace.
233 vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
234 LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr,
235 "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName);
236
237 // Different name is useful for debugging
238 namespace_name = kVendorClassloaderNamespaceName;
239 ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
Inseob Kim67cb0562018-05-04 11:39:12 +0900240 } else {
241 // oem and product public libraries are NOT available to vendor apks, otherwise it
Jiyong Parke0319942018-01-15 21:14:53 +0900242 // would be system->vendor violation.
Inseob Kim67cb0562018-05-04 11:39:12 +0900243 if (!oem_public_libraries_.empty()) {
244 system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_;
245 }
246 if (!product_public_libraries_.empty()) {
247 system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_;
248 }
Jiyong Parka07f3052017-08-22 10:26:10 +0900249 }
250
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800251 NativeLoaderNamespace native_loader_ns;
252 if (!is_native_bridge) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200253 android_namespace_t* android_parent_ns =
254 parent_ns == nullptr ? nullptr : parent_ns->get_android_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900255 android_namespace_t* ns = android_create_namespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800256 nullptr,
257 library_path.c_str(),
258 namespace_type,
259 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200260 android_parent_ns);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800261 if (ns == nullptr) {
262 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200263 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800264 }
265
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700266 // Note that when vendor_ns is not configured this function will return nullptr
267 // and it will result in linking vendor_public_libraries_ to the default namespace
268 // which is expected behavior in this case.
269 android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
270
Jiyong Parka07f3052017-08-22 10:26:10 +0900271 if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800272 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200273 return nullptr;
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800274 }
275
Jiyong Parka07f3052017-08-22 10:26:10 +0900276 if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
277 // vendor apks are allowed to use VNDK-SP libraries.
278 if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
279 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200280 return nullptr;
Jiyong Parka07f3052017-08-22 10:26:10 +0900281 }
282 }
283
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700284 if (!vendor_public_libraries_.empty()) {
285 if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
286 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200287 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700288 }
289 }
290
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800291 native_loader_ns = NativeLoaderNamespace(ns);
292 } else {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200293 native_bridge_namespace_t* native_bridge_parent_namespace =
294 parent_ns == nullptr ? nullptr : parent_ns->get_native_bridge_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900295 native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800296 nullptr,
297 library_path.c_str(),
298 namespace_type,
299 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200300 native_bridge_parent_namespace);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800301 if (ns == nullptr) {
302 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200303 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800304 }
305
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700306 native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace();
307
Jiyong Parka07f3052017-08-22 10:26:10 +0900308 if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800309 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200310 return nullptr;
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800311 }
312
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700313 if (!vendor_public_libraries_.empty()) {
314 if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
315 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200316 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700317 }
318 }
319
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800320 native_loader_ns = NativeLoaderNamespace(ns);
321 }
322
323 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
324
Victor Khimenko1443ec42018-07-09 17:01:22 +0200325 return &(namespaces_.back().second);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800326 }
327
Victor Khimenko1443ec42018-07-09 17:01:22 +0200328 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800329 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800330 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800331 return env->IsSameObject(value.first, class_loader);
332 });
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800333 if (it != namespaces_.end()) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200334 return &it->second;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800335 }
336
Victor Khimenko1443ec42018-07-09 17:01:22 +0200337 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800338 }
339
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700340 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700341 // Once public namespace is initialized there is no
342 // point in running this code - it will have no effect
343 // on the current list of public libraries.
344 if (initialized_) {
345 return;
346 }
347
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700348 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700349 const char* android_root_env = getenv("ANDROID_ROOT");
350 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
351 std::string public_native_libraries_system_config =
352 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Jiyong Parka07f3052017-08-22 10:26:10 +0900353 std::string llndk_native_libraries_system_config =
354 root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
355 std::string vndksp_native_libraries_system_config =
356 root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700357
Inseob Kim67cb0562018-05-04 11:39:12 +0900358 std::string product_public_native_libraries_dir = "/product/etc";
359
Christopher Ferris39da84b2016-06-21 16:11:23 -0700360 std::string error_msg;
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900361 LOG_ALWAYS_FATAL_IF(
362 !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
363 "Error reading public native library list from \"%s\": %s",
364 public_native_libraries_system_config.c_str(), error_msg.c_str());
365
Jiyong Parke0319942018-01-15 21:14:53 +0900366 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
367 // variable to add libraries to the list. This is intended for platform tests only.
368 if (is_debuggable()) {
369 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
370 if (additional_libs != nullptr && additional_libs[0] != '\0') {
371 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
372 std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
373 std::back_inserter(sonames));
374 }
375 }
376
377 // android_init_namespaces() expects all the public libraries
378 // to be loaded so that they can be found by soname alone.
379 //
380 // TODO(dimitry): this is a bit misleading since we do not know
381 // if the vendor public library is going to be opened from /vendor/lib
Inseob Kim67cb0562018-05-04 11:39:12 +0900382 // we might as well end up loading them from /system/lib or /product/lib
Jiyong Parke0319942018-01-15 21:14:53 +0900383 // For now we rely on CTS test to catch things like this but
384 // it should probably be addressed in the future.
385 for (const auto& soname : sonames) {
386 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
387 "Error preloading public library %s: %s", soname.c_str(), dlerror());
388 }
389
390 system_public_libraries_ = base::Join(sonames, ':');
391
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900392 // read /system/etc/public.libraries-<companyname>.txt which contain partner defined
393 // system libs that are exposed to apps. The libs in the txt files must be
394 // named as lib<name>.<companyname>.so.
Jiyong Parke0319942018-01-15 21:14:53 +0900395 sonames.clear();
Inseob Kim67cb0562018-05-04 11:39:12 +0900396 ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames);
Jiyong Parke0319942018-01-15 21:14:53 +0900397 oem_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700398
Inseob Kim67cb0562018-05-04 11:39:12 +0900399 // read /product/etc/public.libraries-<companyname>.txt which contain partner defined
400 // product libs that are exposed to apps.
401 sonames.clear();
402 ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames);
403 product_public_libraries_ = base::Join(sonames, ':');
404
Justin Yun4a1d1102017-11-27 17:04:14 +0900405 // Insert VNDK version to llndk and vndksp config file names.
406 insert_vndk_version_str(&llndk_native_libraries_system_config);
407 insert_vndk_version_str(&vndksp_native_libraries_system_config);
408
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700409 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900410 ReadConfig(llndk_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900411 system_llndk_libraries_ = base::Join(sonames, ':');
412
413 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900414 ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900415 system_vndksp_libraries_ = base::Join(sonames, ':');
416
417 sonames.clear();
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700418 // This file is optional, quietly ignore if the file does not exist.
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900419 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700420
421 vendor_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700422 }
423
Inseob Kim67cb0562018-05-04 11:39:12 +0900424 void Reset() { namespaces_.clear(); }
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700425
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700426 private:
Inseob Kim67cb0562018-05-04 11:39:12 +0900427 void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
428 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
429 if (dir != nullptr) {
430 // Failing to opening the dir is not an error, which can happen in
431 // webview_zygote.
432 while (struct dirent* ent = readdir(dir.get())) {
433 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
434 continue;
435 }
436 const std::string filename(ent->d_name);
437 if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
438 android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
439 const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
440 const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
441 const std::string company_name = filename.substr(start, end - start);
442 const std::string config_file_path = dirname + "/"s + filename;
443 LOG_ALWAYS_FATAL_IF(
444 company_name.empty(),
445 "Error extracting company name from public native library list file path \"%s\"",
446 config_file_path.c_str());
447
448 std::string error_msg;
449
450 LOG_ALWAYS_FATAL_IF(
451 !ReadConfig(
452 config_file_path, sonames,
453 [&company_name](const std::string& soname, std::string* error_msg) {
454 if (android::base::StartsWith(soname, "lib") &&
455 android::base::EndsWith(soname, "." + company_name + ".so")) {
456 return true;
457 } else {
458 *error_msg = "Library name \"" + soname +
459 "\" does not end with the company name: " + company_name + ".";
460 return false;
461 }
462 },
463 &error_msg),
464 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
465 error_msg.c_str());
466 }
467 }
468 }
469 }
470
471
Christopher Ferris39da84b2016-06-21 16:11:23 -0700472 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900473 const std::function<bool(const std::string& /* soname */,
474 std::string* /* error_msg */)>& check_soname,
Christopher Ferris39da84b2016-06-21 16:11:23 -0700475 std::string* error_msg = nullptr) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700476 // Read list of public native libraries from the config file.
477 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700478 if(!base::ReadFileToString(configFile, &file_content)) {
Christopher Ferris39da84b2016-06-21 16:11:23 -0700479 if (error_msg) *error_msg = strerror(errno);
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700480 return false;
481 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700482
483 std::vector<std::string> lines = base::Split(file_content, "\n");
484
Christopher Ferris39da84b2016-06-21 16:11:23 -0700485 for (auto& line : lines) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700486 auto trimmed_line = base::Trim(line);
487 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
488 continue;
489 }
Christopher Ferris39da84b2016-06-21 16:11:23 -0700490 size_t space_pos = trimmed_line.rfind(' ');
491 if (space_pos != std::string::npos) {
492 std::string type = trimmed_line.substr(space_pos + 1);
493 if (type != "32" && type != "64") {
494 if (error_msg) *error_msg = "Malformed line: " + line;
495 return false;
496 }
497#if defined(__LP64__)
498 // Skip 32 bit public library.
499 if (type == "32") {
500 continue;
501 }
502#else
503 // Skip 64 bit public library.
504 if (type == "64") {
505 continue;
506 }
507#endif
508 trimmed_line.resize(space_pos);
509 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700510
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900511 if (check_soname(trimmed_line, error_msg)) {
512 sonames->push_back(trimmed_line);
513 } else {
514 return false;
515 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700516 }
517
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700518 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800519 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800520
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800521 bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
522 // Ask native bride if this apps library path should be handled by it
523 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
524
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700525 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
526 // code is one example) unknown to linker in which case linker uses anonymous
527 // namespace. The second argument specifies the search path for the anonymous
528 // namespace which is the library_path of the classloader.
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700529 initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(),
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800530 is_native_bridge ? nullptr : library_path);
Dimitry Ivanovd836ab02016-11-02 18:03:10 -0700531 if (!initialized_) {
532 *error_msg = dlerror();
533 return false;
534 }
535
536 // and now initialize native bridge namespaces if necessary.
537 if (NativeBridgeInitialized()) {
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700538 initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(),
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800539 is_native_bridge ? library_path : nullptr);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800540 if (!initialized_) {
541 *error_msg = NativeBridgeGetError();
542 }
543 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800544
545 return initialized_;
546 }
547
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700548 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
549 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
550 jmethodID get_parent = env->GetMethodID(class_loader_class,
551 "getParent",
552 "()Ljava/lang/ClassLoader;");
553
554 return env->CallObjectMethod(class_loader, get_parent);
555 }
556
Victor Khimenko1443ec42018-07-09 17:01:22 +0200557 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700558 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
559
560 while (parent_class_loader != nullptr) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200561 NativeLoaderNamespace* ns;
562 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
563 return ns;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700564 }
565
566 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
567 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800568
Victor Khimenko1443ec42018-07-09 17:01:22 +0200569 return nullptr;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700570 }
571
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800572 bool initialized_;
Victor Khimenko1443ec42018-07-09 17:01:22 +0200573 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700574 std::string system_public_libraries_;
575 std::string vendor_public_libraries_;
Jiyong Parke0319942018-01-15 21:14:53 +0900576 std::string oem_public_libraries_;
Inseob Kim67cb0562018-05-04 11:39:12 +0900577 std::string product_public_libraries_;
Jiyong Parka07f3052017-08-22 10:26:10 +0900578 std::string system_llndk_libraries_;
579 std::string system_vndksp_libraries_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800580
581 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
582};
583
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800584static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800585static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
586#endif
587
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700588void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800589#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800590 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700591 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800592#endif
593}
594
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700595void ResetNativeLoader() {
596#if defined(__ANDROID__)
597 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
598 g_namespaces->Reset();
599#endif
600}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800601
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800602jstring CreateClassLoaderNamespace(JNIEnv* env,
603 int32_t target_sdk_version,
604 jobject class_loader,
605 bool is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900606 bool is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800607 jstring library_path,
608 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800609#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800610 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800611
612 std::string error_msg;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800613 bool success = g_namespaces->Create(env,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700614 target_sdk_version,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800615 class_loader,
616 is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900617 is_for_vendor,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800618 library_path,
619 permitted_path,
Victor Khimenko1443ec42018-07-09 17:01:22 +0200620 &error_msg) != nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800621 if (!success) {
622 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800623 }
624#else
Jiyong Parka07f3052017-08-22 10:26:10 +0900625 UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800626 library_path, permitted_path);
627#endif
628 return nullptr;
629}
630
631void* OpenNativeLibrary(JNIEnv* env,
632 int32_t target_sdk_version,
633 const char* path,
634 jobject class_loader,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800635 jstring library_path,
636 bool* needs_native_bridge,
637 std::string* error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800638#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700639 UNUSED(target_sdk_version);
640 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800641 *needs_native_bridge = false;
Pete Bentley632f1422018-12-19 13:33:33 +0000642 void* handle = dlopen(path, RTLD_NOW);
643 if (handle == nullptr) {
644 *error_msg = dlerror();
645 }
646 return handle;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800647 }
648
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800649 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200650 NativeLoaderNamespace* ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800651
Victor Khimenko1443ec42018-07-09 17:01:22 +0200652 if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800653 // This is the case where the classloader was not created by ApplicationLoaders
654 // In this case we create an isolated not-shared namespace for it.
Victor Khimenko1443ec42018-07-09 17:01:22 +0200655 if ((ns = g_namespaces->Create(env,
656 target_sdk_version,
657 class_loader,
658 false /* is_shared */,
659 false /* is_for_vendor */,
660 library_path,
661 nullptr,
662 error_msg)) == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800663 return nullptr;
664 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800665 }
666
Victor Khimenko1443ec42018-07-09 17:01:22 +0200667 return OpenNativeLibrary(ns, path, needs_native_bridge, error_msg);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800668#else
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800669 UNUSED(env, target_sdk_version, class_loader);
670
671 // Do some best effort to emulate library-path support. It will not
672 // work for dependencies.
673 //
674 // Note: null has a special meaning and must be preserved.
675 std::string c_library_path; // Empty string by default.
676 if (library_path != nullptr && path != nullptr && path[0] != '/') {
677 ScopedUtfChars library_path_utf_chars(env, library_path);
678 c_library_path = library_path_utf_chars.c_str();
679 }
680
681 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
682
683 for (const std::string& lib_path : library_paths) {
684 *needs_native_bridge = false;
685 const char* path_arg;
686 std::string complete_path;
687 if (path == nullptr) {
688 // Preserve null.
689 path_arg = nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800690 } else {
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800691 complete_path = lib_path;
692 if (!complete_path.empty()) {
693 complete_path.append("/");
694 }
695 complete_path.append(path);
696 path_arg = complete_path.c_str();
697 }
698 void* handle = dlopen(path_arg, RTLD_NOW);
699 if (handle != nullptr) {
700 return handle;
701 }
702 if (NativeBridgeIsSupported(path_arg)) {
703 *needs_native_bridge = true;
704 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
705 if (handle != nullptr) {
706 return handle;
707 }
708 *error_msg = NativeBridgeGetError();
709 } else {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800710 *error_msg = dlerror();
711 }
712 }
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800713 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800714#endif
715}
716
dimitry3150f7c2018-09-12 01:09:19 +0200717bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, std::string* error_msg) {
718 bool success;
719 if (needs_native_bridge) {
720 success = (NativeBridgeUnloadLibrary(handle) == 0);
721 if (!success) {
722 *error_msg = NativeBridgeGetError();
723 }
724 } else {
725 success = (dlclose(handle) == 0);
726 if (!success) {
727 *error_msg = dlerror();
728 }
729 }
730
731 return success;
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700732}
733
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800734#if defined(__ANDROID__)
Victor Khimenko1443ec42018-07-09 17:01:22 +0200735void* OpenNativeLibrary(NativeLoaderNamespace* ns, const char* path, bool* needs_native_bridge,
736 std::string* error_msg) {
737 if (ns->is_android_namespace()) {
738 android_dlextinfo extinfo;
739 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
740 extinfo.library_namespace = ns->get_android_ns();
741
742 void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
743 if (handle == nullptr) {
744 *error_msg = dlerror();
745 }
746 *needs_native_bridge = false;
747 return handle;
748 } else {
749 void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns());
750 if (handle == nullptr) {
751 *error_msg = NativeBridgeGetError();
752 }
753 *needs_native_bridge = true;
754 return handle;
755 }
756}
757
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700758// native_bridge_namespaces are not supported for callers of this function.
759// This function will return nullptr in the case when application is running
760// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800761android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800762 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200763 NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
764 if (ns != nullptr) {
765 return ns->is_android_namespace() ? ns->get_android_ns() : nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800766 }
767
768 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800769}
Victor Khimenko1443ec42018-07-09 17:01:22 +0200770NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
771 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
772 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
773}
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800774#endif
775
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800776}; // android namespace