xref: /aosp_15_r20/bionic/linker/linker_namespaces.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
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 #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker #include "linker_common_types.h"
32*8d67ca89SAndroid Build Coastguard Worker 
33*8d67ca89SAndroid Build Coastguard Worker #include <string>
34*8d67ca89SAndroid Build Coastguard Worker #include <vector>
35*8d67ca89SAndroid Build Coastguard Worker #include <unordered_set>
36*8d67ca89SAndroid Build Coastguard Worker 
37*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> fix_lib_paths(std::vector<std::string> paths);
38*8d67ca89SAndroid Build Coastguard Worker 
39*8d67ca89SAndroid Build Coastguard Worker struct android_namespace_t;
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker struct android_namespace_link_t {
42*8d67ca89SAndroid Build Coastguard Worker  public:
android_namespace_link_tandroid_namespace_link_t43*8d67ca89SAndroid Build Coastguard Worker   android_namespace_link_t(android_namespace_t* linked_namespace,
44*8d67ca89SAndroid Build Coastguard Worker                            std::unordered_set<std::string> shared_lib_sonames,
45*8d67ca89SAndroid Build Coastguard Worker                            bool allow_all_shared_libs)
46*8d67ca89SAndroid Build Coastguard Worker       : linked_namespace_(linked_namespace),
47*8d67ca89SAndroid Build Coastguard Worker         shared_lib_sonames_(std::move(shared_lib_sonames)),
48*8d67ca89SAndroid Build Coastguard Worker         allow_all_shared_libs_(allow_all_shared_libs) {}
49*8d67ca89SAndroid Build Coastguard Worker 
linked_namespaceandroid_namespace_link_t50*8d67ca89SAndroid Build Coastguard Worker   android_namespace_t* linked_namespace() const {
51*8d67ca89SAndroid Build Coastguard Worker     return linked_namespace_;
52*8d67ca89SAndroid Build Coastguard Worker   }
53*8d67ca89SAndroid Build Coastguard Worker 
shared_lib_sonamesandroid_namespace_link_t54*8d67ca89SAndroid Build Coastguard Worker   const std::unordered_set<std::string>& shared_lib_sonames() const {
55*8d67ca89SAndroid Build Coastguard Worker     return shared_lib_sonames_;
56*8d67ca89SAndroid Build Coastguard Worker   }
57*8d67ca89SAndroid Build Coastguard Worker 
is_accessibleandroid_namespace_link_t58*8d67ca89SAndroid Build Coastguard Worker   bool is_accessible(const char* soname) const {
59*8d67ca89SAndroid Build Coastguard Worker     return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end();
60*8d67ca89SAndroid Build Coastguard Worker   }
61*8d67ca89SAndroid Build Coastguard Worker 
allow_all_shared_libsandroid_namespace_link_t62*8d67ca89SAndroid Build Coastguard Worker   bool allow_all_shared_libs() const {
63*8d67ca89SAndroid Build Coastguard Worker     return allow_all_shared_libs_;
64*8d67ca89SAndroid Build Coastguard Worker   }
65*8d67ca89SAndroid Build Coastguard Worker 
66*8d67ca89SAndroid Build Coastguard Worker  private:
67*8d67ca89SAndroid Build Coastguard Worker   android_namespace_t* const linked_namespace_;
68*8d67ca89SAndroid Build Coastguard Worker   const std::unordered_set<std::string> shared_lib_sonames_;
69*8d67ca89SAndroid Build Coastguard Worker   bool allow_all_shared_libs_;
70*8d67ca89SAndroid Build Coastguard Worker };
71*8d67ca89SAndroid Build Coastguard Worker 
72*8d67ca89SAndroid Build Coastguard Worker struct android_namespace_t {
73*8d67ca89SAndroid Build Coastguard Worker  public:
android_namespace_tandroid_namespace_t74*8d67ca89SAndroid Build Coastguard Worker   android_namespace_t() :
75*8d67ca89SAndroid Build Coastguard Worker     is_isolated_(false),
76*8d67ca89SAndroid Build Coastguard Worker     is_exempt_list_enabled_(false),
77*8d67ca89SAndroid Build Coastguard Worker     is_also_used_as_anonymous_(false) {}
78*8d67ca89SAndroid Build Coastguard Worker 
get_nameandroid_namespace_t79*8d67ca89SAndroid Build Coastguard Worker   const char* get_name() const { return name_.c_str(); }
set_nameandroid_namespace_t80*8d67ca89SAndroid Build Coastguard Worker   void set_name(const char* name) { name_ = name; }
81*8d67ca89SAndroid Build Coastguard Worker 
is_isolatedandroid_namespace_t82*8d67ca89SAndroid Build Coastguard Worker   bool is_isolated() const { return is_isolated_; }
set_isolatedandroid_namespace_t83*8d67ca89SAndroid Build Coastguard Worker   void set_isolated(bool isolated) { is_isolated_ = isolated; }
84*8d67ca89SAndroid Build Coastguard Worker 
is_exempt_list_enabledandroid_namespace_t85*8d67ca89SAndroid Build Coastguard Worker   bool is_exempt_list_enabled() const { return is_exempt_list_enabled_; }
set_exempt_list_enabledandroid_namespace_t86*8d67ca89SAndroid Build Coastguard Worker   void set_exempt_list_enabled(bool enabled) { is_exempt_list_enabled_ = enabled; }
87*8d67ca89SAndroid Build Coastguard Worker 
is_also_used_as_anonymousandroid_namespace_t88*8d67ca89SAndroid Build Coastguard Worker   bool is_also_used_as_anonymous() const { return is_also_used_as_anonymous_; }
set_also_used_as_anonymousandroid_namespace_t89*8d67ca89SAndroid Build Coastguard Worker   void set_also_used_as_anonymous(bool yes) { is_also_used_as_anonymous_ = yes; }
90*8d67ca89SAndroid Build Coastguard Worker 
get_ld_library_pathsandroid_namespace_t91*8d67ca89SAndroid Build Coastguard Worker   const std::vector<std::string>& get_ld_library_paths() const {
92*8d67ca89SAndroid Build Coastguard Worker     return ld_library_paths_;
93*8d67ca89SAndroid Build Coastguard Worker   }
set_ld_library_pathsandroid_namespace_t94*8d67ca89SAndroid Build Coastguard Worker   void set_ld_library_paths(std::vector<std::string>&& library_paths) {
95*8d67ca89SAndroid Build Coastguard Worker     ld_library_paths_ = std::move(library_paths);
96*8d67ca89SAndroid Build Coastguard Worker   }
97*8d67ca89SAndroid Build Coastguard Worker 
get_default_library_pathsandroid_namespace_t98*8d67ca89SAndroid Build Coastguard Worker   const std::vector<std::string>& get_default_library_paths() const {
99*8d67ca89SAndroid Build Coastguard Worker     return default_library_paths_;
100*8d67ca89SAndroid Build Coastguard Worker   }
set_default_library_pathsandroid_namespace_t101*8d67ca89SAndroid Build Coastguard Worker   void set_default_library_paths(std::vector<std::string>&& library_paths) {
102*8d67ca89SAndroid Build Coastguard Worker     default_library_paths_ = fix_lib_paths(std::move(library_paths));
103*8d67ca89SAndroid Build Coastguard Worker   }
set_default_library_pathsandroid_namespace_t104*8d67ca89SAndroid Build Coastguard Worker   void set_default_library_paths(const std::vector<std::string>& library_paths) {
105*8d67ca89SAndroid Build Coastguard Worker     default_library_paths_ = fix_lib_paths(library_paths);
106*8d67ca89SAndroid Build Coastguard Worker   }
107*8d67ca89SAndroid Build Coastguard Worker 
get_permitted_pathsandroid_namespace_t108*8d67ca89SAndroid Build Coastguard Worker   const std::vector<std::string>& get_permitted_paths() const {
109*8d67ca89SAndroid Build Coastguard Worker     return permitted_paths_;
110*8d67ca89SAndroid Build Coastguard Worker   }
set_permitted_pathsandroid_namespace_t111*8d67ca89SAndroid Build Coastguard Worker   void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
112*8d67ca89SAndroid Build Coastguard Worker     permitted_paths_ = std::move(permitted_paths);
113*8d67ca89SAndroid Build Coastguard Worker   }
set_permitted_pathsandroid_namespace_t114*8d67ca89SAndroid Build Coastguard Worker   void set_permitted_paths(const std::vector<std::string>& permitted_paths) {
115*8d67ca89SAndroid Build Coastguard Worker     permitted_paths_ = permitted_paths;
116*8d67ca89SAndroid Build Coastguard Worker   }
117*8d67ca89SAndroid Build Coastguard Worker 
get_allowed_libsandroid_namespace_t118*8d67ca89SAndroid Build Coastguard Worker   const std::vector<std::string>& get_allowed_libs() const { return allowed_libs_; }
set_allowed_libsandroid_namespace_t119*8d67ca89SAndroid Build Coastguard Worker   void set_allowed_libs(std::vector<std::string>&& allowed_libs) {
120*8d67ca89SAndroid Build Coastguard Worker     allowed_libs_ = std::move(allowed_libs);
121*8d67ca89SAndroid Build Coastguard Worker   }
set_allowed_libsandroid_namespace_t122*8d67ca89SAndroid Build Coastguard Worker   void set_allowed_libs(const std::vector<std::string>& allowed_libs) {
123*8d67ca89SAndroid Build Coastguard Worker     allowed_libs_ = allowed_libs;
124*8d67ca89SAndroid Build Coastguard Worker   }
125*8d67ca89SAndroid Build Coastguard Worker 
linked_namespacesandroid_namespace_t126*8d67ca89SAndroid Build Coastguard Worker   const std::vector<android_namespace_link_t>& linked_namespaces() const {
127*8d67ca89SAndroid Build Coastguard Worker     return linked_namespaces_;
128*8d67ca89SAndroid Build Coastguard Worker   }
add_linked_namespaceandroid_namespace_t129*8d67ca89SAndroid Build Coastguard Worker   void add_linked_namespace(android_namespace_t* linked_namespace,
130*8d67ca89SAndroid Build Coastguard Worker                             std::unordered_set<std::string> shared_lib_sonames,
131*8d67ca89SAndroid Build Coastguard Worker                             bool allow_all_shared_libs) {
132*8d67ca89SAndroid Build Coastguard Worker     linked_namespaces_.emplace_back(linked_namespace, std::move(shared_lib_sonames),
133*8d67ca89SAndroid Build Coastguard Worker                                     allow_all_shared_libs);
134*8d67ca89SAndroid Build Coastguard Worker   }
135*8d67ca89SAndroid Build Coastguard Worker 
add_soinfoandroid_namespace_t136*8d67ca89SAndroid Build Coastguard Worker   void add_soinfo(soinfo* si) {
137*8d67ca89SAndroid Build Coastguard Worker     soinfo_list_.push_back(si);
138*8d67ca89SAndroid Build Coastguard Worker   }
139*8d67ca89SAndroid Build Coastguard Worker 
add_soinfosandroid_namespace_t140*8d67ca89SAndroid Build Coastguard Worker   void add_soinfos(const soinfo_list_t& soinfos) {
141*8d67ca89SAndroid Build Coastguard Worker     for (auto si : soinfos) {
142*8d67ca89SAndroid Build Coastguard Worker       add_soinfo(si);
143*8d67ca89SAndroid Build Coastguard Worker     }
144*8d67ca89SAndroid Build Coastguard Worker   }
145*8d67ca89SAndroid Build Coastguard Worker 
remove_soinfoandroid_namespace_t146*8d67ca89SAndroid Build Coastguard Worker   void remove_soinfo(soinfo* si) {
147*8d67ca89SAndroid Build Coastguard Worker     soinfo_list_.remove_if([&](soinfo* candidate) {
148*8d67ca89SAndroid Build Coastguard Worker       return si == candidate;
149*8d67ca89SAndroid Build Coastguard Worker     });
150*8d67ca89SAndroid Build Coastguard Worker   }
151*8d67ca89SAndroid Build Coastguard Worker 
soinfo_listandroid_namespace_t152*8d67ca89SAndroid Build Coastguard Worker   const soinfo_list_t& soinfo_list() const { return soinfo_list_; }
153*8d67ca89SAndroid Build Coastguard Worker 
154*8d67ca89SAndroid Build Coastguard Worker   // For isolated namespaces - checks if the file is on the search path;
155*8d67ca89SAndroid Build Coastguard Worker   // always returns true for not isolated namespace.
156*8d67ca89SAndroid Build Coastguard Worker   bool is_accessible(const std::string& path);
157*8d67ca89SAndroid Build Coastguard Worker 
158*8d67ca89SAndroid Build Coastguard Worker   // Returns true if si is accessible from this namespace. A soinfo
159*8d67ca89SAndroid Build Coastguard Worker   // is considered accessible when it belongs to this namespace
160*8d67ca89SAndroid Build Coastguard Worker   // or one of it's parent soinfos belongs to this namespace.
161*8d67ca89SAndroid Build Coastguard Worker   bool is_accessible(soinfo* si);
162*8d67ca89SAndroid Build Coastguard Worker 
163*8d67ca89SAndroid Build Coastguard Worker   soinfo_list_t get_global_group();
164*8d67ca89SAndroid Build Coastguard Worker   soinfo_list_t get_shared_group();
165*8d67ca89SAndroid Build Coastguard Worker 
166*8d67ca89SAndroid Build Coastguard Worker  private:
167*8d67ca89SAndroid Build Coastguard Worker   std::string name_;
168*8d67ca89SAndroid Build Coastguard Worker   bool is_isolated_;
169*8d67ca89SAndroid Build Coastguard Worker   bool is_exempt_list_enabled_;
170*8d67ca89SAndroid Build Coastguard Worker   bool is_also_used_as_anonymous_;
171*8d67ca89SAndroid Build Coastguard Worker   std::vector<std::string> ld_library_paths_;
172*8d67ca89SAndroid Build Coastguard Worker   std::vector<std::string> default_library_paths_;
173*8d67ca89SAndroid Build Coastguard Worker   std::vector<std::string> permitted_paths_;
174*8d67ca89SAndroid Build Coastguard Worker   std::vector<std::string> allowed_libs_;
175*8d67ca89SAndroid Build Coastguard Worker   // Loader looks into linked namespace if it was not able
176*8d67ca89SAndroid Build Coastguard Worker   // to find a library in this namespace. Note that library
177*8d67ca89SAndroid Build Coastguard Worker   // lookup in linked namespaces are limited by the list of
178*8d67ca89SAndroid Build Coastguard Worker   // shared sonames.
179*8d67ca89SAndroid Build Coastguard Worker   std::vector<android_namespace_link_t> linked_namespaces_;
180*8d67ca89SAndroid Build Coastguard Worker   soinfo_list_t soinfo_list_;
181*8d67ca89SAndroid Build Coastguard Worker 
182*8d67ca89SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
183*8d67ca89SAndroid Build Coastguard Worker };
184