2015-12-01 22:56:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nativeloader/native_loader.h"
|
|
|
|
#include "ScopedUtfChars.h"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#ifdef __ANDROID__
|
2016-05-10 02:51:51 +02:00
|
|
|
#include "dlext_namespaces.h"
|
2015-12-01 22:56:44 +01:00
|
|
|
#include "cutils/properties.h"
|
2016-02-23 23:23:51 +01:00
|
|
|
#include "log/log.h"
|
2015-12-01 22:56:44 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
#include "android-base/file.h"
|
2015-12-08 19:38:59 +01:00
|
|
|
#include "android-base/macros.h"
|
|
|
|
#include "android-base/strings.h"
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-04-22 01:42:48 +02:00
|
|
|
static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt";
|
2016-04-07 03:24:08 +02:00
|
|
|
static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-10 19:39:48 +02:00
|
|
|
// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
|
|
|
|
// System.load() with an absolute path which is outside of the classloader library search path.
|
|
|
|
// This list includes all directories app is allowed to access this way.
|
|
|
|
static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
|
|
|
|
|
2016-05-06 02:30:24 +02:00
|
|
|
static bool is_debuggable() {
|
|
|
|
char debuggable[PROP_VALUE_MAX];
|
|
|
|
property_get("ro.debuggable", debuggable, "0");
|
|
|
|
return std::string(debuggable) == "1";
|
|
|
|
}
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
class LibraryNamespaces {
|
|
|
|
public:
|
2016-02-22 20:27:48 +01:00
|
|
|
LibraryNamespaces() : initialized_(false) { }
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-02-23 23:23:51 +01:00
|
|
|
android_namespace_t* Create(JNIEnv* env,
|
|
|
|
jobject class_loader,
|
|
|
|
bool is_shared,
|
|
|
|
jstring java_library_path,
|
|
|
|
jstring java_permitted_path) {
|
2016-05-09 19:55:50 +02:00
|
|
|
std::string library_path; // empty string by default.
|
|
|
|
|
|
|
|
if (java_library_path != nullptr) {
|
|
|
|
ScopedUtfChars library_path_utf_chars(env, java_library_path);
|
|
|
|
library_path = library_path_utf_chars.c_str();
|
|
|
|
}
|
2015-12-08 20:16:56 +01:00
|
|
|
|
2016-05-10 19:39:48 +02:00
|
|
|
// (http://b/27588281) This is a workaround for apps using custom
|
|
|
|
// classloaders and calling System.load() with an absolute path which
|
|
|
|
// is outside of the classloader library search path.
|
|
|
|
//
|
|
|
|
// This part effectively allows such a classloader to access anything
|
|
|
|
// under /data and /mnt/expand
|
|
|
|
std::string permitted_path = kWhitelistedDirectories;
|
|
|
|
|
2015-12-08 20:16:56 +01:00
|
|
|
if (java_permitted_path != nullptr) {
|
|
|
|
ScopedUtfChars path(env, java_permitted_path);
|
2016-05-11 01:21:25 +02:00
|
|
|
if (path.c_str() != nullptr && path.size() > 0) {
|
|
|
|
permitted_path = permitted_path + ":" + path.c_str();
|
|
|
|
}
|
2015-12-08 20:16:56 +01:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2015-12-08 20:16:56 +01:00
|
|
|
if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
|
2015-12-01 22:56:44 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:02:35 +01:00
|
|
|
android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
LOG_ALWAYS_FATAL_IF(ns != nullptr,
|
|
|
|
"There is already a namespace associated with this classloader");
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2015-12-15 20:06:57 +01:00
|
|
|
uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
|
|
|
|
if (is_shared) {
|
|
|
|
namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:02:35 +01:00
|
|
|
ns = android_create_namespace("classloader-namespace",
|
|
|
|
nullptr,
|
|
|
|
library_path.c_str(),
|
|
|
|
namespace_type,
|
2016-05-17 02:08:06 +02:00
|
|
|
permitted_path.c_str());
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-02-23 23:23:51 +01:00
|
|
|
if (ns != nullptr) {
|
|
|
|
namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:48:22 +01:00
|
|
|
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
|
|
|
|
auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
|
|
|
|
[&](const std::pair<jweak, android_namespace_t*>& value) {
|
|
|
|
return env->IsSameObject(value.first, class_loader);
|
|
|
|
});
|
|
|
|
return it != namespaces_.end() ? it->second : nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
void Initialize() {
|
2016-05-10 03:12:00 +02:00
|
|
|
// Once public namespace is initialized there is no
|
|
|
|
// point in running this code - it will have no effect
|
|
|
|
// on the current list of public libraries.
|
|
|
|
if (initialized_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
std::vector<std::string> sonames;
|
2016-04-22 01:42:48 +02:00
|
|
|
const char* android_root_env = getenv("ANDROID_ROOT");
|
|
|
|
std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
|
|
|
|
std::string public_native_libraries_system_config =
|
|
|
|
root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
|
2016-04-07 03:24:08 +02:00
|
|
|
|
2016-04-22 01:42:48 +02:00
|
|
|
LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
|
2016-04-07 03:24:08 +02:00
|
|
|
"Error reading public native library list from \"%s\": %s",
|
2016-04-22 01:42:48 +02:00
|
|
|
public_native_libraries_system_config.c_str(), strerror(errno));
|
2016-05-06 02:30:24 +02:00
|
|
|
|
|
|
|
// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
|
|
|
|
// variable to add libraries to the list. This is intended for platform tests only.
|
|
|
|
if (is_debuggable()) {
|
|
|
|
const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
|
|
|
|
if (additional_libs != nullptr && additional_libs[0] != '\0') {
|
|
|
|
std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
|
|
|
|
std::copy(additional_libs_vector.begin(),
|
|
|
|
additional_libs_vector.end(),
|
|
|
|
std::back_inserter(sonames));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
// This file is optional, quietly ignore if the file does not exist.
|
|
|
|
ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
|
|
|
|
|
|
|
|
// android_init_namespaces() expects all the public libraries
|
|
|
|
// to be loaded so that they can be found by soname alone.
|
|
|
|
//
|
|
|
|
// TODO(dimitry): this is a bit misleading since we do not know
|
|
|
|
// if the vendor public library is going to be opened from /vendor/lib
|
|
|
|
// we might as well end up loading them from /system/lib
|
|
|
|
// For now we rely on CTS test to catch things like this but
|
|
|
|
// it should probably be addressed in the future.
|
|
|
|
for (const auto& soname : sonames) {
|
|
|
|
dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public_libraries_ = base::Join(sonames, ':');
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:43:16 +02:00
|
|
|
void Reset() {
|
|
|
|
namespaces_.clear();
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
private:
|
|
|
|
bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
|
2016-03-15 21:51:26 +01:00
|
|
|
// Read list of public native libraries from the config file.
|
|
|
|
std::string file_content;
|
2016-04-07 03:24:08 +02:00
|
|
|
if(!base::ReadFileToString(configFile, &file_content)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-15 21:51:26 +01:00
|
|
|
|
|
|
|
std::vector<std::string> lines = base::Split(file_content, "\n");
|
|
|
|
|
|
|
|
for (const auto& line : lines) {
|
|
|
|
auto trimmed_line = base::Trim(line);
|
|
|
|
if (trimmed_line[0] == '#' || trimmed_line.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
sonames->push_back(trimmed_line);
|
2016-03-15 21:51:26 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
return true;
|
2016-02-10 23:09:22 +01:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-02-10 23:09:22 +01:00
|
|
|
bool InitPublicNamespace(const char* library_path) {
|
2016-03-15 21:51:26 +01:00
|
|
|
// (http://b/25844435) - Some apps call dlopen from generated code (mono jited
|
|
|
|
// code is one example) unknown to linker in which case linker uses anonymous
|
|
|
|
// namespace. The second argument specifies the search path for the anonymous
|
|
|
|
// namespace which is the library_path of the classloader.
|
|
|
|
initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
return initialized_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initialized_;
|
|
|
|
std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
|
2016-03-15 21:51:26 +01:00
|
|
|
std::string public_libraries_;
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
|
|
|
|
};
|
|
|
|
|
2016-02-29 22:21:43 +01:00
|
|
|
static std::mutex g_namespaces_mutex;
|
2015-12-01 22:56:44 +01:00
|
|
|
static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
|
|
|
|
#endif
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
void InitializeNativeLoader() {
|
2016-02-22 20:27:48 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-03-15 21:51:26 +01:00
|
|
|
g_namespaces->Initialize();
|
2016-02-22 20:27:48 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:43:16 +02:00
|
|
|
void ResetNativeLoader() {
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
|
|
|
g_namespaces->Reset();
|
|
|
|
#endif
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-02-23 23:23:51 +01:00
|
|
|
jstring CreateClassLoaderNamespace(JNIEnv* env,
|
|
|
|
int32_t target_sdk_version,
|
|
|
|
jobject class_loader,
|
|
|
|
bool is_shared,
|
|
|
|
jstring library_path,
|
|
|
|
jstring permitted_path) {
|
2015-12-01 22:56:44 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-04-21 01:07:30 +02:00
|
|
|
UNUSED(target_sdk_version);
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-02-23 23:23:51 +01:00
|
|
|
android_namespace_t* ns = g_namespaces->Create(env,
|
|
|
|
class_loader,
|
|
|
|
is_shared,
|
|
|
|
library_path,
|
|
|
|
permitted_path);
|
|
|
|
if (ns == nullptr) {
|
|
|
|
return env->NewStringUTF(dlerror());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
UNUSED(env, target_sdk_version, class_loader, is_shared,
|
|
|
|
library_path, permitted_path);
|
|
|
|
#endif
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* OpenNativeLibrary(JNIEnv* env,
|
|
|
|
int32_t target_sdk_version,
|
|
|
|
const char* path,
|
|
|
|
jobject class_loader,
|
|
|
|
jstring library_path) {
|
|
|
|
#if defined(__ANDROID__)
|
2016-04-21 01:07:30 +02:00
|
|
|
UNUSED(target_sdk_version);
|
|
|
|
if (class_loader == nullptr) {
|
2015-12-01 22:56:44 +01:00
|
|
|
return dlopen(path, RTLD_NOW);
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-02-23 23:23:51 +01:00
|
|
|
android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
if (ns == nullptr) {
|
2016-02-23 23:23:51 +01:00
|
|
|
// This is the case where the classloader was not created by ApplicationLoaders
|
|
|
|
// In this case we create an isolated not-shared namespace for it.
|
|
|
|
ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
|
|
|
|
if (ns == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
android_dlextinfo extinfo;
|
|
|
|
extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
|
|
|
|
extinfo.library_namespace = ns;
|
|
|
|
|
|
|
|
return android_dlopen_ext(path, RTLD_NOW, &extinfo);
|
|
|
|
#else
|
2016-02-23 23:23:51 +01:00
|
|
|
UNUSED(env, target_sdk_version, class_loader, library_path);
|
2015-12-01 22:56:44 +01:00
|
|
|
return dlopen(path, RTLD_NOW);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:55:25 +02:00
|
|
|
bool CloseNativeLibrary(void* handle) {
|
|
|
|
return dlclose(handle) == 0;
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:48:22 +01:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-02-22 22:48:22 +01:00
|
|
|
return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
}; // android namespace
|