| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 The Android Open Source Project | 
 | 3 |  * All rights reserved. | 
 | 4 |  * | 
 | 5 |  * Redistribution and use in source and binary forms, with or without | 
 | 6 |  * modification, are permitted provided that the following conditions | 
 | 7 |  * are met: | 
 | 8 |  *  * Redistributions of source code must retain the above copyright | 
 | 9 |  *    notice, this list of conditions and the following disclaimer. | 
 | 10 |  *  * Redistributions in binary form must reproduce the above copyright | 
 | 11 |  *    notice, this list of conditions and the following disclaimer in | 
 | 12 |  *    the documentation and/or other materials provided with the | 
 | 13 |  *    distribution. | 
 | 14 |  * | 
 | 15 |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
 | 16 |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
 | 17 |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
 | 18 |  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
 | 19 |  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
 | 20 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
 | 21 |  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
 | 22 |  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
 | 23 |  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
 | 24 |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
 | 25 |  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
 | 26 |  * SUCH DAMAGE. | 
 | 27 |  */ | 
 | 28 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 30 |  | 
 | 31 | #include "linker_common_types.h" | 
 | 32 |  | 
| Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 33 | #include <string> | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 34 | #include <vector> | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 35 | #include <unordered_set> | 
 | 36 |  | 
| Ryan Prichard | 4049440 | 2020-03-24 22:37:50 -0700 | [diff] [blame] | 37 | std::vector<std::string> fix_lib_paths(std::vector<std::string> paths); | 
 | 38 |  | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 39 | struct android_namespace_t; | 
 | 40 |  | 
 | 41 | struct android_namespace_link_t { | 
 | 42 |  public: | 
 | 43 |   android_namespace_link_t(android_namespace_t* linked_namespace, | 
| Jooyung Han | 57b03de | 2022-12-01 16:23:03 +0900 | [diff] [blame] | 44 |                            std::unordered_set<std::string> shared_lib_sonames, | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 45 |                            bool allow_all_shared_libs) | 
| Jooyung Han | 57b03de | 2022-12-01 16:23:03 +0900 | [diff] [blame] | 46 |       : linked_namespace_(linked_namespace), | 
 | 47 |         shared_lib_sonames_(std::move(shared_lib_sonames)), | 
 | 48 |         allow_all_shared_libs_(allow_all_shared_libs) {} | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 49 |  | 
 | 50 |   android_namespace_t* linked_namespace() const { | 
 | 51 |     return linked_namespace_; | 
 | 52 |   } | 
 | 53 |  | 
| Dimitry Ivanov | f1cb669 | 2017-05-01 17:45:38 -0700 | [diff] [blame] | 54 |   const std::unordered_set<std::string>& shared_lib_sonames() const { | 
 | 55 |     return shared_lib_sonames_; | 
 | 56 |   } | 
 | 57 |  | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 58 |   bool is_accessible(const char* soname) const { | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 59 |     return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end(); | 
 | 60 |   } | 
 | 61 |  | 
 | 62 |   bool allow_all_shared_libs() const { | 
 | 63 |     return allow_all_shared_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 64 |   } | 
 | 65 |  | 
 | 66 |  private: | 
 | 67 |   android_namespace_t* const linked_namespace_; | 
 | 68 |   const std::unordered_set<std::string> shared_lib_sonames_; | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 69 |   bool allow_all_shared_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 70 | }; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 71 |  | 
 | 72 | struct android_namespace_t { | 
 | 73 |  public: | 
| Jiyong Park | 25bedfd | 2019-05-16 21:00:39 +0900 | [diff] [blame] | 74 |   android_namespace_t() : | 
 | 75 |     is_isolated_(false), | 
| Ryan Prichard | aff9a34 | 2020-08-03 15:29:12 -0700 | [diff] [blame] | 76 |     is_exempt_list_enabled_(false), | 
| Jiyong Park | 25bedfd | 2019-05-16 21:00:39 +0900 | [diff] [blame] | 77 |     is_also_used_as_anonymous_(false) {} | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 78 |  | 
| Jiyong Park | b66a78b | 2019-05-20 11:04:18 +0900 | [diff] [blame] | 79 |   const char* get_name() const { return name_.c_str(); } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 80 |   void set_name(const char* name) { name_ = name; } | 
 | 81 |  | 
 | 82 |   bool is_isolated() const { return is_isolated_; } | 
 | 83 |   void set_isolated(bool isolated) { is_isolated_ = isolated; } | 
 | 84 |  | 
| Ryan Prichard | aff9a34 | 2020-08-03 15:29:12 -0700 | [diff] [blame] | 85 |   bool is_exempt_list_enabled() const { return is_exempt_list_enabled_; } | 
 | 86 |   void set_exempt_list_enabled(bool enabled) { is_exempt_list_enabled_ = enabled; } | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 87 |  | 
| Jiyong Park | 25bedfd | 2019-05-16 21:00:39 +0900 | [diff] [blame] | 88 |   bool is_also_used_as_anonymous() const { return is_also_used_as_anonymous_; } | 
 | 89 |   void set_also_used_as_anonymous(bool yes) { is_also_used_as_anonymous_ = yes; } | 
 | 90 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 91 |   const std::vector<std::string>& get_ld_library_paths() const { | 
 | 92 |     return ld_library_paths_; | 
 | 93 |   } | 
 | 94 |   void set_ld_library_paths(std::vector<std::string>&& library_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 95 |     ld_library_paths_ = std::move(library_paths); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 96 |   } | 
 | 97 |  | 
 | 98 |   const std::vector<std::string>& get_default_library_paths() const { | 
 | 99 |     return default_library_paths_; | 
 | 100 |   } | 
 | 101 |   void set_default_library_paths(std::vector<std::string>&& library_paths) { | 
| Ryan Prichard | 4049440 | 2020-03-24 22:37:50 -0700 | [diff] [blame] | 102 |     default_library_paths_ = fix_lib_paths(std::move(library_paths)); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 103 |   } | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 104 |   void set_default_library_paths(const std::vector<std::string>& library_paths) { | 
| Ryan Prichard | 4049440 | 2020-03-24 22:37:50 -0700 | [diff] [blame] | 105 |     default_library_paths_ = fix_lib_paths(library_paths); | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 106 |   } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 107 |  | 
 | 108 |   const std::vector<std::string>& get_permitted_paths() const { | 
 | 109 |     return permitted_paths_; | 
 | 110 |   } | 
 | 111 |   void set_permitted_paths(std::vector<std::string>&& permitted_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 112 |     permitted_paths_ = std::move(permitted_paths); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 113 |   } | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 114 |   void set_permitted_paths(const std::vector<std::string>& permitted_paths) { | 
 | 115 |     permitted_paths_ = permitted_paths; | 
 | 116 |   } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 117 |  | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 118 |   const std::vector<std::string>& get_allowed_libs() const { return allowed_libs_; } | 
 | 119 |   void set_allowed_libs(std::vector<std::string>&& allowed_libs) { | 
 | 120 |     allowed_libs_ = std::move(allowed_libs); | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 121 |   } | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 122 |   void set_allowed_libs(const std::vector<std::string>& allowed_libs) { | 
 | 123 |     allowed_libs_ = allowed_libs; | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 124 |   } | 
 | 125 |  | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 126 |   const std::vector<android_namespace_link_t>& linked_namespaces() const { | 
 | 127 |     return linked_namespaces_; | 
 | 128 |   } | 
 | 129 |   void add_linked_namespace(android_namespace_t* linked_namespace, | 
| Jooyung Han | 57b03de | 2022-12-01 16:23:03 +0900 | [diff] [blame] | 130 |                             std::unordered_set<std::string> shared_lib_sonames, | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 131 |                             bool allow_all_shared_libs) { | 
| Jooyung Han | 57b03de | 2022-12-01 16:23:03 +0900 | [diff] [blame] | 132 |     linked_namespaces_.emplace_back(linked_namespace, std::move(shared_lib_sonames), | 
 | 133 |                                     allow_all_shared_libs); | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 134 |   } | 
 | 135 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 136 |   void add_soinfo(soinfo* si) { | 
 | 137 |     soinfo_list_.push_back(si); | 
 | 138 |   } | 
 | 139 |  | 
 | 140 |   void add_soinfos(const soinfo_list_t& soinfos) { | 
 | 141 |     for (auto si : soinfos) { | 
 | 142 |       add_soinfo(si); | 
 | 143 |     } | 
 | 144 |   } | 
 | 145 |  | 
 | 146 |   void remove_soinfo(soinfo* si) { | 
 | 147 |     soinfo_list_.remove_if([&](soinfo* candidate) { | 
 | 148 |       return si == candidate; | 
 | 149 |     }); | 
 | 150 |   } | 
 | 151 |  | 
 | 152 |   const soinfo_list_t& soinfo_list() const { return soinfo_list_; } | 
 | 153 |  | 
 | 154 |   // For isolated namespaces - checks if the file is on the search path; | 
 | 155 |   // always returns true for not isolated namespace. | 
 | 156 |   bool is_accessible(const std::string& path); | 
 | 157 |  | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 158 |   // Returns true if si is accessible from this namespace. A soinfo | 
 | 159 |   // is considered accessible when it belongs to this namespace | 
 | 160 |   // or one of it's parent soinfos belongs to this namespace. | 
 | 161 |   bool is_accessible(soinfo* si); | 
 | 162 |  | 
| Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 163 |   soinfo_list_t get_global_group(); | 
 | 164 |   soinfo_list_t get_shared_group(); | 
 | 165 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 166 |  private: | 
| Jiyong Park | b66a78b | 2019-05-20 11:04:18 +0900 | [diff] [blame] | 167 |   std::string name_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 168 |   bool is_isolated_; | 
| Ryan Prichard | aff9a34 | 2020-08-03 15:29:12 -0700 | [diff] [blame] | 169 |   bool is_exempt_list_enabled_; | 
| Jiyong Park | 25bedfd | 2019-05-16 21:00:39 +0900 | [diff] [blame] | 170 |   bool is_also_used_as_anonymous_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 171 |   std::vector<std::string> ld_library_paths_; | 
 | 172 |   std::vector<std::string> default_library_paths_; | 
 | 173 |   std::vector<std::string> permitted_paths_; | 
| Luke Huang | 30f2f05 | 2020-07-30 15:09:18 +0800 | [diff] [blame] | 174 |   std::vector<std::string> allowed_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 175 |   // Loader looks into linked namespace if it was not able | 
 | 176 |   // to find a library in this namespace. Note that library | 
 | 177 |   // lookup in linked namespaces are limited by the list of | 
 | 178 |   // shared sonames. | 
 | 179 |   std::vector<android_namespace_link_t> linked_namespaces_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 180 |   soinfo_list_t soinfo_list_; | 
 | 181 |  | 
 | 182 |   DISALLOW_COPY_AND_ASSIGN(android_namespace_t); | 
 | 183 | }; |