Fix dlsym() to take into account RTLD_GLOBAL/LOCAL
Symbols from libraries opened with RTLD_LOCAL (default)
should not be visible via dlsym(RLTD_DEFAULT/RTLD_NEXT, .)
Bug: 17512583
Bug: 18186310
(cherry picked from commit e8ba50fe0d51fbefee1a8f5bb62bf51d841512c8)
Change-Id: Idf6bbe2233fb2bfc0c88677e7d1fc518fb3f7a8b
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 3631d2f..59f673a 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -232,7 +232,7 @@
static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
#endif
-static soinfo __libdl_info("libdl.so", nullptr, 0);
+static soinfo __libdl_info("libdl.so", nullptr, 0, RTLD_GLOBAL);
// This is used by the dynamic linker. Every process gets these symbols for free.
soinfo* get_libdl_info() {
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 37e0189..bd05498 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -282,13 +282,13 @@
g_soinfo_links_allocator.protect_all(protection);
}
-static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset) {
+static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, int rtld_flags) {
if (strlen(name) >= SOINFO_NAME_LEN) {
DL_ERR("library name \"%s\" too long", name);
return nullptr;
}
- soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset);
+ soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
sonext->next = si;
sonext = si;
@@ -453,7 +453,7 @@
return nullptr;
}
-soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset) {
+soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
memset(this, 0, sizeof(*this));
strlcpy(this->name, name, sizeof(this->name));
@@ -465,6 +465,8 @@
this->st_ino = file_stat->st_ino;
this->file_offset = file_offset;
}
+
+ this->rtld_flags = rtld_flags;
}
static unsigned elfhash(const char* _name) {
@@ -716,6 +718,10 @@
ElfW(Sym)* s = nullptr;
for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
+ if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
+ continue;
+ }
+
s = soinfo_elf_lookup(si, elf_hash, name);
if (s != nullptr) {
*found = si;
@@ -806,7 +812,7 @@
}
}
-static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int dlflags, const android_dlextinfo* extinfo) {
+static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
int fd = -1;
off64_t file_offset = 0;
ScopedFd file_guard(-1);
@@ -851,7 +857,7 @@
}
}
- if ((dlflags & RTLD_NOLOAD) != 0) {
+ if ((rtld_flags & RTLD_NOLOAD) != 0) {
DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
return nullptr;
}
@@ -862,7 +868,7 @@
return nullptr;
}
- soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset);
+ soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
if (si == nullptr) {
return nullptr;
}
@@ -894,7 +900,7 @@
return nullptr;
}
-static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int dlflags, const android_dlextinfo* extinfo) {
+static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
soinfo* si = find_loaded_library_by_name(name);
@@ -902,7 +908,7 @@
// of this fact is done by load_library.
if (si == nullptr) {
TRACE("[ '%s' has not been found by name. Trying harder...]", name);
- si = load_library(load_tasks, name, dlflags, extinfo);
+ si = load_library(load_tasks, name, rtld_flags, extinfo);
}
return si;
@@ -926,7 +932,7 @@
}
static bool find_libraries(const char* const library_names[], size_t library_names_size, soinfo* soinfos[],
- soinfo* ld_preloads[], size_t ld_preloads_size, int dlflags, const android_dlextinfo* extinfo) {
+ soinfo* ld_preloads[], size_t ld_preloads_size, int rtld_flags, const android_dlextinfo* extinfo) {
// Step 0: prepare.
LoadTaskList load_tasks;
for (size_t i = 0; i < library_names_size; ++i) {
@@ -952,7 +958,7 @@
// Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
for (LoadTask::unique_ptr task(load_tasks.pop_front()); task.get() != nullptr; task.reset(load_tasks.pop_front())) {
- soinfo* si = find_library_internal(load_tasks, task->get_name(), dlflags, extinfo);
+ soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
if (si == nullptr) {
return false;
}
@@ -997,7 +1003,7 @@
return true;
}
-static soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
+static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
if (name == nullptr) {
somain->ref_count++;
return somain;
@@ -1005,7 +1011,7 @@
soinfo* si;
- if (!find_libraries(&name, 1, &si, nullptr, 0, dlflags, extinfo)) {
+ if (!find_libraries(&name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
return nullptr;
}
@@ -1790,6 +1796,14 @@
return 0;
}
+int soinfo::get_rtld_flags() {
+ if (has_min_version(1)) {
+ return rtld_flags;
+ }
+
+ return 0;
+}
+
// This is a return on get_children()/get_parents() if
// 'this->flags' does not have FLAG_NEW_SOINFO set.
static soinfo::soinfo_list_t g_empty_list;
@@ -2210,7 +2224,7 @@
return;
}
- soinfo* si = soinfo_alloc("[vdso]", nullptr, 0);
+ soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
si->phnum = ehdr_vdso->e_phnum;
@@ -2231,7 +2245,7 @@
#else
#define LINKER_PATH "/system/bin/linker"
#endif
-static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0);
+static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
/* gdb expects the linker to be in the debug shared object list.
* Without this, gdb has trouble locating the linker's ".text"
@@ -2295,7 +2309,7 @@
INFO("[ android linker & debugger ]");
- soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0);
+ soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
if (si == nullptr) {
exit(EXIT_FAILURE);
}
@@ -2370,7 +2384,7 @@
memset(needed_library_names, 0, sizeof(needed_library_names));
needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
- if (needed_libraries_count > 0 && !find_libraries(needed_library_names, needed_libraries_count, needed_library_si, g_ld_preloads, ld_preloads_count, 0, nullptr)) {
+ if (needed_libraries_count > 0 && !find_libraries(needed_library_names, needed_libraries_count, needed_library_si, g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) {
__libc_format_fd(2, "CANNOT LINK EXECUTABLE DEPENDENCIES: %s\n", linker_get_error_buffer());
exit(EXIT_FAILURE);
}
@@ -2483,7 +2497,7 @@
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
- soinfo linker_so("[dynamic linker]", nullptr, 0);
+ soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
// If the linker is not acting as PT_INTERP entry_point is equal to
// _start. Which means that the linker is running as an executable and
diff --git a/linker/linker.h b/linker/linker.h
index 3b140ac..ef2fbcd 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -199,7 +199,7 @@
#endif
bool has_DT_SYMBOLIC;
- soinfo(const char* name, const struct stat* file_stat, off64_t file_offset);
+ soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags);
void CallConstructors();
void CallDestructors();
@@ -214,6 +214,8 @@
dev_t get_st_dev();
off64_t get_file_offset();
+ int get_rtld_flags();
+
soinfo_list_t& get_children();
soinfo_list_t& get_parents();
@@ -246,6 +248,7 @@
// version >= 1
off64_t file_offset;
+ int rtld_flags;
};
extern soinfo* get_libdl_info();
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 9c9fbdd..a55b364 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -202,6 +202,42 @@
dlclose(handle);
}
+TEST(dlfcn, dlopen_check_rtld_local) {
+ void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym == nullptr);
+
+ // implicit RTLD_LOCAL
+ void* handle = dlopen("libtest_simple.so", RTLD_NOW);
+ sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym == nullptr);
+ ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
+ sym = dlsym(handle, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym != nullptr);
+ ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
+ dlclose(handle);
+
+ // explicit RTLD_LOCAL
+ handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
+ sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym == nullptr);
+ ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
+ sym = dlsym(handle, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym != nullptr);
+ ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
+ dlclose(handle);
+}
+
+TEST(dlfcn, dlopen_check_rtld_global) {
+ void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym == nullptr);
+
+ void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
+ sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
+ ASSERT_TRUE(sym != nullptr) << dlerror();
+ ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
+ dlclose(handle);
+}
+
// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
// libtest_with_dependency_loop_a.so
diff --git a/tests/libs/dlopen_testlib_simple.cpp b/tests/libs/dlopen_testlib_simple.cpp
index afe54b4..06253e1 100644
--- a/tests/libs/dlopen_testlib_simple.cpp
+++ b/tests/libs/dlopen_testlib_simple.cpp
@@ -18,6 +18,6 @@
uint32_t dlopen_testlib_taxicab_number = 1729;
-bool dlopen_testlib_simple_func() {
+extern "C" bool dlopen_testlib_simple_func() {
return true;
}