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 29 #pragma once 30 31 #include "linker_common_types.h" 32 33 #include <string> 34 #include <vector> 35 #include <unordered_set> 36 37 std::vector<std::string> fix_lib_paths(std::vector<std::string> paths); 38 39 struct android_namespace_t; 40 41 struct android_namespace_link_t { 42 public: android_namespace_link_tandroid_namespace_link_t43 android_namespace_link_t(android_namespace_t* linked_namespace, 44 std::unordered_set<std::string> shared_lib_sonames, 45 bool allow_all_shared_libs) 46 : linked_namespace_(linked_namespace), 47 shared_lib_sonames_(std::move(shared_lib_sonames)), 48 allow_all_shared_libs_(allow_all_shared_libs) {} 49 linked_namespaceandroid_namespace_link_t50 android_namespace_t* linked_namespace() const { 51 return linked_namespace_; 52 } 53 shared_lib_sonamesandroid_namespace_link_t54 const std::unordered_set<std::string>& shared_lib_sonames() const { 55 return shared_lib_sonames_; 56 } 57 is_accessibleandroid_namespace_link_t58 bool is_accessible(const char* soname) const { 59 return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end(); 60 } 61 allow_all_shared_libsandroid_namespace_link_t62 bool allow_all_shared_libs() const { 63 return allow_all_shared_libs_; 64 } 65 66 private: 67 android_namespace_t* const linked_namespace_; 68 const std::unordered_set<std::string> shared_lib_sonames_; 69 bool allow_all_shared_libs_; 70 }; 71 72 struct android_namespace_t { 73 public: android_namespace_tandroid_namespace_t74 android_namespace_t() : 75 is_isolated_(false), 76 is_exempt_list_enabled_(false), 77 is_also_used_as_anonymous_(false) {} 78 get_nameandroid_namespace_t79 const char* get_name() const { return name_.c_str(); } set_nameandroid_namespace_t80 void set_name(const char* name) { name_ = name; } 81 is_isolatedandroid_namespace_t82 bool is_isolated() const { return is_isolated_; } set_isolatedandroid_namespace_t83 void set_isolated(bool isolated) { is_isolated_ = isolated; } 84 is_exempt_list_enabledandroid_namespace_t85 bool is_exempt_list_enabled() const { return is_exempt_list_enabled_; } set_exempt_list_enabledandroid_namespace_t86 void set_exempt_list_enabled(bool enabled) { is_exempt_list_enabled_ = enabled; } 87 is_also_used_as_anonymousandroid_namespace_t88 bool is_also_used_as_anonymous() const { return is_also_used_as_anonymous_; } set_also_used_as_anonymousandroid_namespace_t89 void set_also_used_as_anonymous(bool yes) { is_also_used_as_anonymous_ = yes; } 90 get_ld_library_pathsandroid_namespace_t91 const std::vector<std::string>& get_ld_library_paths() const { 92 return ld_library_paths_; 93 } set_ld_library_pathsandroid_namespace_t94 void set_ld_library_paths(std::vector<std::string>&& library_paths) { 95 ld_library_paths_ = std::move(library_paths); 96 } 97 get_default_library_pathsandroid_namespace_t98 const std::vector<std::string>& get_default_library_paths() const { 99 return default_library_paths_; 100 } set_default_library_pathsandroid_namespace_t101 void set_default_library_paths(std::vector<std::string>&& library_paths) { 102 default_library_paths_ = fix_lib_paths(std::move(library_paths)); 103 } set_default_library_pathsandroid_namespace_t104 void set_default_library_paths(const std::vector<std::string>& library_paths) { 105 default_library_paths_ = fix_lib_paths(library_paths); 106 } 107 get_permitted_pathsandroid_namespace_t108 const std::vector<std::string>& get_permitted_paths() const { 109 return permitted_paths_; 110 } set_permitted_pathsandroid_namespace_t111 void set_permitted_paths(std::vector<std::string>&& permitted_paths) { 112 permitted_paths_ = std::move(permitted_paths); 113 } set_permitted_pathsandroid_namespace_t114 void set_permitted_paths(const std::vector<std::string>& permitted_paths) { 115 permitted_paths_ = permitted_paths; 116 } 117 get_allowed_libsandroid_namespace_t118 const std::vector<std::string>& get_allowed_libs() const { return allowed_libs_; } set_allowed_libsandroid_namespace_t119 void set_allowed_libs(std::vector<std::string>&& allowed_libs) { 120 allowed_libs_ = std::move(allowed_libs); 121 } set_allowed_libsandroid_namespace_t122 void set_allowed_libs(const std::vector<std::string>& allowed_libs) { 123 allowed_libs_ = allowed_libs; 124 } 125 linked_namespacesandroid_namespace_t126 const std::vector<android_namespace_link_t>& linked_namespaces() const { 127 return linked_namespaces_; 128 } add_linked_namespaceandroid_namespace_t129 void add_linked_namespace(android_namespace_t* linked_namespace, 130 std::unordered_set<std::string> shared_lib_sonames, 131 bool allow_all_shared_libs) { 132 linked_namespaces_.emplace_back(linked_namespace, std::move(shared_lib_sonames), 133 allow_all_shared_libs); 134 } 135 add_soinfoandroid_namespace_t136 void add_soinfo(soinfo* si) { 137 soinfo_list_.push_back(si); 138 } 139 add_soinfosandroid_namespace_t140 void add_soinfos(const soinfo_list_t& soinfos) { 141 for (auto si : soinfos) { 142 add_soinfo(si); 143 } 144 } 145 remove_soinfoandroid_namespace_t146 void remove_soinfo(soinfo* si) { 147 soinfo_list_.remove_if([&](soinfo* candidate) { 148 return si == candidate; 149 }); 150 } 151 soinfo_listandroid_namespace_t152 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 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 163 soinfo_list_t get_global_group(); 164 soinfo_list_t get_shared_group(); 165 166 private: 167 std::string name_; 168 bool is_isolated_; 169 bool is_exempt_list_enabled_; 170 bool is_also_used_as_anonymous_; 171 std::vector<std::string> ld_library_paths_; 172 std::vector<std::string> default_library_paths_; 173 std::vector<std::string> permitted_paths_; 174 std::vector<std::string> allowed_libs_; 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_; 180 soinfo_list_t soinfo_list_; 181 182 DISALLOW_COPY_AND_ASSIGN(android_namespace_t); 183 }; 184