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 struct android_namespace_t; 38 39 struct android_namespace_link_t { 40 public: android_namespace_link_tandroid_namespace_link_t41 android_namespace_link_t(android_namespace_t* linked_namespace, 42 const std::unordered_set<std::string>& shared_lib_sonames, 43 bool allow_all_shared_libs) 44 : linked_namespace_(linked_namespace), shared_lib_sonames_(shared_lib_sonames), 45 allow_all_shared_libs_(allow_all_shared_libs) 46 {} 47 linked_namespaceandroid_namespace_link_t48 android_namespace_t* linked_namespace() const { 49 return linked_namespace_; 50 } 51 shared_lib_sonamesandroid_namespace_link_t52 const std::unordered_set<std::string>& shared_lib_sonames() const { 53 return shared_lib_sonames_; 54 } 55 is_accessibleandroid_namespace_link_t56 bool is_accessible(const char* soname) const { 57 if (soname == nullptr) { 58 return false; 59 } 60 return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end(); 61 } 62 allow_all_shared_libsandroid_namespace_link_t63 bool allow_all_shared_libs() const { 64 return allow_all_shared_libs_; 65 } 66 67 private: 68 android_namespace_t* const linked_namespace_; 69 const std::unordered_set<std::string> shared_lib_sonames_; 70 bool allow_all_shared_libs_; 71 }; 72 73 struct android_namespace_t { 74 public: android_namespace_tandroid_namespace_t75 android_namespace_t() : 76 is_isolated_(false), 77 is_greylist_enabled_(false), 78 is_also_used_as_anonymous_(false) {} 79 get_nameandroid_namespace_t80 const char* get_name() const { return name_.c_str(); } set_nameandroid_namespace_t81 void set_name(const char* name) { name_ = name; } 82 is_isolatedandroid_namespace_t83 bool is_isolated() const { return is_isolated_; } set_isolatedandroid_namespace_t84 void set_isolated(bool isolated) { is_isolated_ = isolated; } 85 is_greylist_enabledandroid_namespace_t86 bool is_greylist_enabled() const { return is_greylist_enabled_; } set_greylist_enabledandroid_namespace_t87 void set_greylist_enabled(bool enabled) { is_greylist_enabled_ = enabled; } 88 is_also_used_as_anonymousandroid_namespace_t89 bool is_also_used_as_anonymous() const { return is_also_used_as_anonymous_; } set_also_used_as_anonymousandroid_namespace_t90 void set_also_used_as_anonymous(bool yes) { is_also_used_as_anonymous_ = yes; } 91 get_ld_library_pathsandroid_namespace_t92 const std::vector<std::string>& get_ld_library_paths() const { 93 return ld_library_paths_; 94 } set_ld_library_pathsandroid_namespace_t95 void set_ld_library_paths(std::vector<std::string>&& library_paths) { 96 ld_library_paths_ = std::move(library_paths); 97 } 98 get_default_library_pathsandroid_namespace_t99 const std::vector<std::string>& get_default_library_paths() const { 100 return default_library_paths_; 101 } set_default_library_pathsandroid_namespace_t102 void set_default_library_paths(std::vector<std::string>&& library_paths) { 103 default_library_paths_ = std::move(library_paths); 104 } set_default_library_pathsandroid_namespace_t105 void set_default_library_paths(const std::vector<std::string>& library_paths) { 106 default_library_paths_ = library_paths; 107 } 108 get_permitted_pathsandroid_namespace_t109 const std::vector<std::string>& get_permitted_paths() const { 110 return permitted_paths_; 111 } set_permitted_pathsandroid_namespace_t112 void set_permitted_paths(std::vector<std::string>&& permitted_paths) { 113 permitted_paths_ = std::move(permitted_paths); 114 } set_permitted_pathsandroid_namespace_t115 void set_permitted_paths(const std::vector<std::string>& permitted_paths) { 116 permitted_paths_ = permitted_paths; 117 } 118 get_whitelisted_libsandroid_namespace_t119 const std::vector<std::string>& get_whitelisted_libs() const { 120 return whitelisted_libs_; 121 } set_whitelisted_libsandroid_namespace_t122 void set_whitelisted_libs(std::vector<std::string>&& whitelisted_libs) { 123 whitelisted_libs_ = std::move(whitelisted_libs); 124 } set_whitelisted_libsandroid_namespace_t125 void set_whitelisted_libs(const std::vector<std::string>& whitelisted_libs) { 126 whitelisted_libs_ = whitelisted_libs; 127 } 128 linked_namespacesandroid_namespace_t129 const std::vector<android_namespace_link_t>& linked_namespaces() const { 130 return linked_namespaces_; 131 } add_linked_namespaceandroid_namespace_t132 void add_linked_namespace(android_namespace_t* linked_namespace, 133 const std::unordered_set<std::string>& shared_lib_sonames, 134 bool allow_all_shared_libs) { 135 linked_namespaces_.push_back( 136 android_namespace_link_t(linked_namespace, shared_lib_sonames, allow_all_shared_libs)); 137 } 138 add_soinfoandroid_namespace_t139 void add_soinfo(soinfo* si) { 140 soinfo_list_.push_back(si); 141 } 142 add_soinfosandroid_namespace_t143 void add_soinfos(const soinfo_list_t& soinfos) { 144 for (auto si : soinfos) { 145 add_soinfo(si); 146 } 147 } 148 remove_soinfoandroid_namespace_t149 void remove_soinfo(soinfo* si) { 150 soinfo_list_.remove_if([&](soinfo* candidate) { 151 return si == candidate; 152 }); 153 } 154 soinfo_listandroid_namespace_t155 const soinfo_list_t& soinfo_list() const { return soinfo_list_; } 156 157 // For isolated namespaces - checks if the file is on the search path; 158 // always returns true for not isolated namespace. 159 bool is_accessible(const std::string& path); 160 161 // Returns true if si is accessible from this namespace. A soinfo 162 // is considered accessible when it belongs to this namespace 163 // or one of it's parent soinfos belongs to this namespace. 164 bool is_accessible(soinfo* si); 165 166 soinfo_list_t get_global_group(); 167 soinfo_list_t get_shared_group(); 168 169 private: 170 std::string name_; 171 bool is_isolated_; 172 bool is_greylist_enabled_; 173 bool is_also_used_as_anonymous_; 174 std::vector<std::string> ld_library_paths_; 175 std::vector<std::string> default_library_paths_; 176 std::vector<std::string> permitted_paths_; 177 std::vector<std::string> whitelisted_libs_; 178 // Loader looks into linked namespace if it was not able 179 // to find a library in this namespace. Note that library 180 // lookup in linked namespaces are limited by the list of 181 // shared sonames. 182 std::vector<android_namespace_link_t> linked_namespaces_; 183 soinfo_list_t soinfo_list_; 184 185 DISALLOW_COPY_AND_ASSIGN(android_namespace_t); 186 }; 187