1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include "linker_namespaces.h"
30*8d67ca89SAndroid Build Coastguard Worker #include "linker_globals.h"
31*8d67ca89SAndroid Build Coastguard Worker #include "linker_soinfo.h"
32*8d67ca89SAndroid Build Coastguard Worker #include "linker_utils.h"
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker #include <dlfcn.h>
35*8d67ca89SAndroid Build Coastguard Worker
36*8d67ca89SAndroid Build Coastguard Worker // Given an absolute path, can this library be loaded into this namespace?
is_accessible(const std::string & file)37*8d67ca89SAndroid Build Coastguard Worker bool android_namespace_t::is_accessible(const std::string& file) {
38*8d67ca89SAndroid Build Coastguard Worker if (!is_isolated_) {
39*8d67ca89SAndroid Build Coastguard Worker return true;
40*8d67ca89SAndroid Build Coastguard Worker }
41*8d67ca89SAndroid Build Coastguard Worker
42*8d67ca89SAndroid Build Coastguard Worker if (!allowed_libs_.empty()) {
43*8d67ca89SAndroid Build Coastguard Worker const char *lib_name = basename(file.c_str());
44*8d67ca89SAndroid Build Coastguard Worker if (std::find(allowed_libs_.begin(), allowed_libs_.end(), lib_name) == allowed_libs_.end()) {
45*8d67ca89SAndroid Build Coastguard Worker return false;
46*8d67ca89SAndroid Build Coastguard Worker }
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker for (const auto& dir : ld_library_paths_) {
50*8d67ca89SAndroid Build Coastguard Worker if (file_is_in_dir(file, dir)) {
51*8d67ca89SAndroid Build Coastguard Worker return true;
52*8d67ca89SAndroid Build Coastguard Worker }
53*8d67ca89SAndroid Build Coastguard Worker }
54*8d67ca89SAndroid Build Coastguard Worker
55*8d67ca89SAndroid Build Coastguard Worker for (const auto& dir : default_library_paths_) {
56*8d67ca89SAndroid Build Coastguard Worker if (file_is_in_dir(file, dir)) {
57*8d67ca89SAndroid Build Coastguard Worker return true;
58*8d67ca89SAndroid Build Coastguard Worker }
59*8d67ca89SAndroid Build Coastguard Worker }
60*8d67ca89SAndroid Build Coastguard Worker
61*8d67ca89SAndroid Build Coastguard Worker for (const auto& dir : permitted_paths_) {
62*8d67ca89SAndroid Build Coastguard Worker if (file_is_under_dir(file, dir)) {
63*8d67ca89SAndroid Build Coastguard Worker return true;
64*8d67ca89SAndroid Build Coastguard Worker }
65*8d67ca89SAndroid Build Coastguard Worker }
66*8d67ca89SAndroid Build Coastguard Worker
67*8d67ca89SAndroid Build Coastguard Worker return false;
68*8d67ca89SAndroid Build Coastguard Worker }
69*8d67ca89SAndroid Build Coastguard Worker
70*8d67ca89SAndroid Build Coastguard Worker // Are symbols from this shared object accessible for symbol lookups in a library from this
71*8d67ca89SAndroid Build Coastguard Worker // namespace?
is_accessible(soinfo * s)72*8d67ca89SAndroid Build Coastguard Worker bool android_namespace_t::is_accessible(soinfo* s) {
73*8d67ca89SAndroid Build Coastguard Worker auto is_accessible_ftor = [this] (soinfo* si, bool allow_secondary) {
74*8d67ca89SAndroid Build Coastguard Worker // This is workaround for apps hacking into soinfo list.
75*8d67ca89SAndroid Build Coastguard Worker // and inserting their own entries into it. (http://b/37191433)
76*8d67ca89SAndroid Build Coastguard Worker if (!si->has_min_version(3)) {
77*8d67ca89SAndroid Build Coastguard Worker DL_WARN("Warning: invalid soinfo version for \"%s\" (assuming inaccessible)",
78*8d67ca89SAndroid Build Coastguard Worker si->get_soname());
79*8d67ca89SAndroid Build Coastguard Worker return false;
80*8d67ca89SAndroid Build Coastguard Worker }
81*8d67ca89SAndroid Build Coastguard Worker
82*8d67ca89SAndroid Build Coastguard Worker if (si->get_primary_namespace() == this) {
83*8d67ca89SAndroid Build Coastguard Worker return true;
84*8d67ca89SAndroid Build Coastguard Worker }
85*8d67ca89SAndroid Build Coastguard Worker
86*8d67ca89SAndroid Build Coastguard Worker // When we're looking up symbols, we want to search libraries from the same namespace (whether
87*8d67ca89SAndroid Build Coastguard Worker // the namespace membership is primary or secondary), but we also want to search the immediate
88*8d67ca89SAndroid Build Coastguard Worker // dependencies of libraries in our namespace. (e.g. Supposing that libapp.so -> libandroid.so
89*8d67ca89SAndroid Build Coastguard Worker // crosses a namespace boundary, we want to search libandroid.so but not any of libandroid.so's
90*8d67ca89SAndroid Build Coastguard Worker // dependencies).
91*8d67ca89SAndroid Build Coastguard Worker //
92*8d67ca89SAndroid Build Coastguard Worker // Some libraries may be present in this namespace via the secondary namespace list:
93*8d67ca89SAndroid Build Coastguard Worker // - the executable
94*8d67ca89SAndroid Build Coastguard Worker // - LD_PRELOAD and DF_1_GLOBAL libraries
95*8d67ca89SAndroid Build Coastguard Worker // - libraries inherited during dynamic namespace creation (e.g. because of
96*8d67ca89SAndroid Build Coastguard Worker // RTLD_GLOBAL / DF_1_GLOBAL / ANDROID_NAMESPACE_TYPE_SHARED)
97*8d67ca89SAndroid Build Coastguard Worker //
98*8d67ca89SAndroid Build Coastguard Worker // When a library's membership is secondary, we want to search its symbols, but not the symbols
99*8d67ca89SAndroid Build Coastguard Worker // of its dependencies. The executable may depend on internal system libraries which should not
100*8d67ca89SAndroid Build Coastguard Worker // be searched.
101*8d67ca89SAndroid Build Coastguard Worker if (allow_secondary) {
102*8d67ca89SAndroid Build Coastguard Worker const android_namespace_list_t& secondary_namespaces = si->get_secondary_namespaces();
103*8d67ca89SAndroid Build Coastguard Worker if (secondary_namespaces.contains(this)) {
104*8d67ca89SAndroid Build Coastguard Worker return true;
105*8d67ca89SAndroid Build Coastguard Worker }
106*8d67ca89SAndroid Build Coastguard Worker }
107*8d67ca89SAndroid Build Coastguard Worker
108*8d67ca89SAndroid Build Coastguard Worker return false;
109*8d67ca89SAndroid Build Coastguard Worker };
110*8d67ca89SAndroid Build Coastguard Worker
111*8d67ca89SAndroid Build Coastguard Worker if (is_accessible_ftor(s, true)) {
112*8d67ca89SAndroid Build Coastguard Worker return true;
113*8d67ca89SAndroid Build Coastguard Worker }
114*8d67ca89SAndroid Build Coastguard Worker
115*8d67ca89SAndroid Build Coastguard Worker return !s->get_parents().visit([&](soinfo* si) {
116*8d67ca89SAndroid Build Coastguard Worker return !is_accessible_ftor(si, false);
117*8d67ca89SAndroid Build Coastguard Worker });
118*8d67ca89SAndroid Build Coastguard Worker }
119*8d67ca89SAndroid Build Coastguard Worker
120*8d67ca89SAndroid Build Coastguard Worker // TODO: this is slightly unusual way to construct
121*8d67ca89SAndroid Build Coastguard Worker // the global group for relocation. Not every RTLD_GLOBAL
122*8d67ca89SAndroid Build Coastguard Worker // library is included in this group for backwards-compatibility
123*8d67ca89SAndroid Build Coastguard Worker // reasons.
124*8d67ca89SAndroid Build Coastguard Worker //
125*8d67ca89SAndroid Build Coastguard Worker // This group consists of the main executable, LD_PRELOADs
126*8d67ca89SAndroid Build Coastguard Worker // and libraries with the DF_1_GLOBAL flag set.
get_global_group()127*8d67ca89SAndroid Build Coastguard Worker soinfo_list_t android_namespace_t::get_global_group() {
128*8d67ca89SAndroid Build Coastguard Worker soinfo_list_t global_group;
129*8d67ca89SAndroid Build Coastguard Worker soinfo_list().for_each([&](soinfo* si) {
130*8d67ca89SAndroid Build Coastguard Worker if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
131*8d67ca89SAndroid Build Coastguard Worker global_group.push_back(si);
132*8d67ca89SAndroid Build Coastguard Worker }
133*8d67ca89SAndroid Build Coastguard Worker });
134*8d67ca89SAndroid Build Coastguard Worker
135*8d67ca89SAndroid Build Coastguard Worker return global_group;
136*8d67ca89SAndroid Build Coastguard Worker }
137*8d67ca89SAndroid Build Coastguard Worker
138*8d67ca89SAndroid Build Coastguard Worker // This function provides a list of libraries to be shared
139*8d67ca89SAndroid Build Coastguard Worker // by the namespace. For the default namespace this is the global
140*8d67ca89SAndroid Build Coastguard Worker // group (see get_global_group). For all others this is a group
141*8d67ca89SAndroid Build Coastguard Worker // of RTLD_GLOBAL libraries (which includes the global group from
142*8d67ca89SAndroid Build Coastguard Worker // the default namespace).
get_shared_group()143*8d67ca89SAndroid Build Coastguard Worker soinfo_list_t android_namespace_t::get_shared_group() {
144*8d67ca89SAndroid Build Coastguard Worker if (this == &g_default_namespace) {
145*8d67ca89SAndroid Build Coastguard Worker return get_global_group();
146*8d67ca89SAndroid Build Coastguard Worker }
147*8d67ca89SAndroid Build Coastguard Worker
148*8d67ca89SAndroid Build Coastguard Worker soinfo_list_t shared_group;
149*8d67ca89SAndroid Build Coastguard Worker soinfo_list().for_each([&](soinfo* si) {
150*8d67ca89SAndroid Build Coastguard Worker if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
151*8d67ca89SAndroid Build Coastguard Worker shared_group.push_back(si);
152*8d67ca89SAndroid Build Coastguard Worker }
153*8d67ca89SAndroid Build Coastguard Worker });
154*8d67ca89SAndroid Build Coastguard Worker
155*8d67ca89SAndroid Build Coastguard Worker return shared_group;
156*8d67ca89SAndroid Build Coastguard Worker }
157