| 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 |  | 
|  | 37 | struct android_namespace_t; | 
|  | 38 |  | 
|  | 39 | struct android_namespace_link_t { | 
|  | 40 | public: | 
|  | 41 | android_namespace_link_t(android_namespace_t* linked_namespace, | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 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) | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 46 | {} | 
|  | 47 |  | 
|  | 48 | android_namespace_t* linked_namespace() const { | 
|  | 49 | return linked_namespace_; | 
|  | 50 | } | 
|  | 51 |  | 
| Dimitry Ivanov | f1cb669 | 2017-05-01 17:45:38 -0700 | [diff] [blame] | 52 | const std::unordered_set<std::string>& shared_lib_sonames() const { | 
|  | 53 | return shared_lib_sonames_; | 
|  | 54 | } | 
|  | 55 |  | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 56 | bool is_accessible(const char* soname) const { | 
| dimitry | 94f7a87 | 2018-04-27 12:19:07 +0200 | [diff] [blame] | 57 | if (soname == nullptr) { | 
|  | 58 | return false; | 
|  | 59 | } | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 60 | return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end(); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | bool allow_all_shared_libs() const { | 
|  | 64 | return allow_all_shared_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | private: | 
|  | 68 | android_namespace_t* const linked_namespace_; | 
|  | 69 | const std::unordered_set<std::string> shared_lib_sonames_; | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 70 | bool allow_all_shared_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 71 | }; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | struct android_namespace_t { | 
|  | 74 | public: | 
| Jiyong Park | b66a78b | 2019-05-20 11:04:18 +0900 | [diff] [blame] | 75 | android_namespace_t() : is_isolated_(false), is_greylist_enabled_(false) {} | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 76 |  | 
| Jiyong Park | b66a78b | 2019-05-20 11:04:18 +0900 | [diff] [blame] | 77 | const char* get_name() const { return name_.c_str(); } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 78 | void set_name(const char* name) { name_ = name; } | 
|  | 79 |  | 
|  | 80 | bool is_isolated() const { return is_isolated_; } | 
|  | 81 | void set_isolated(bool isolated) { is_isolated_ = isolated; } | 
|  | 82 |  | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 83 | bool is_greylist_enabled() const { return is_greylist_enabled_; } | 
|  | 84 | void set_greylist_enabled(bool enabled) { is_greylist_enabled_ = enabled; } | 
|  | 85 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 86 | const std::vector<std::string>& get_ld_library_paths() const { | 
|  | 87 | return ld_library_paths_; | 
|  | 88 | } | 
|  | 89 | void set_ld_library_paths(std::vector<std::string>&& library_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 90 | ld_library_paths_ = std::move(library_paths); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | const std::vector<std::string>& get_default_library_paths() const { | 
|  | 94 | return default_library_paths_; | 
|  | 95 | } | 
|  | 96 | void set_default_library_paths(std::vector<std::string>&& library_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 97 | default_library_paths_ = std::move(library_paths); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 98 | } | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 99 | void set_default_library_paths(const std::vector<std::string>& library_paths) { | 
|  | 100 | default_library_paths_ = library_paths; | 
|  | 101 | } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | const std::vector<std::string>& get_permitted_paths() const { | 
|  | 104 | return permitted_paths_; | 
|  | 105 | } | 
|  | 106 | void set_permitted_paths(std::vector<std::string>&& permitted_paths) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 107 | permitted_paths_ = std::move(permitted_paths); | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 108 | } | 
| Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 109 | void set_permitted_paths(const std::vector<std::string>& permitted_paths) { | 
|  | 110 | permitted_paths_ = permitted_paths; | 
|  | 111 | } | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 112 |  | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 113 | const std::vector<std::string>& get_whitelisted_libs() const { | 
|  | 114 | return whitelisted_libs_; | 
|  | 115 | } | 
|  | 116 | void set_whitelisted_libs(std::vector<std::string>&& whitelisted_libs) { | 
| Vic Yang | 976d4b4 | 2019-03-12 13:38:30 -0700 | [diff] [blame] | 117 | whitelisted_libs_ = std::move(whitelisted_libs); | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 118 | } | 
|  | 119 | void set_whitelisted_libs(const std::vector<std::string>& whitelisted_libs) { | 
|  | 120 | whitelisted_libs_ = whitelisted_libs; | 
|  | 121 | } | 
|  | 122 |  | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 123 | const std::vector<android_namespace_link_t>& linked_namespaces() const { | 
|  | 124 | return linked_namespaces_; | 
|  | 125 | } | 
|  | 126 | void add_linked_namespace(android_namespace_t* linked_namespace, | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 127 | const std::unordered_set<std::string>& shared_lib_sonames, | 
|  | 128 | bool allow_all_shared_libs) { | 
|  | 129 | linked_namespaces_.push_back( | 
|  | 130 | android_namespace_link_t(linked_namespace, shared_lib_sonames, allow_all_shared_libs)); | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 133 | void add_soinfo(soinfo* si) { | 
|  | 134 | soinfo_list_.push_back(si); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | void add_soinfos(const soinfo_list_t& soinfos) { | 
|  | 138 | for (auto si : soinfos) { | 
|  | 139 | add_soinfo(si); | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | void remove_soinfo(soinfo* si) { | 
|  | 144 | soinfo_list_.remove_if([&](soinfo* candidate) { | 
|  | 145 | return si == candidate; | 
|  | 146 | }); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | const soinfo_list_t& soinfo_list() const { return soinfo_list_; } | 
|  | 150 |  | 
|  | 151 | // For isolated namespaces - checks if the file is on the search path; | 
|  | 152 | // always returns true for not isolated namespace. | 
|  | 153 | bool is_accessible(const std::string& path); | 
|  | 154 |  | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 155 | // Returns true if si is accessible from this namespace. A soinfo | 
|  | 156 | // is considered accessible when it belongs to this namespace | 
|  | 157 | // or one of it's parent soinfos belongs to this namespace. | 
|  | 158 | bool is_accessible(soinfo* si); | 
|  | 159 |  | 
| Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 160 | soinfo_list_t get_global_group(); | 
|  | 161 | soinfo_list_t get_shared_group(); | 
|  | 162 |  | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 163 | private: | 
| Jiyong Park | b66a78b | 2019-05-20 11:04:18 +0900 | [diff] [blame] | 164 | std::string name_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 165 | bool is_isolated_; | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 166 | bool is_greylist_enabled_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 167 | std::vector<std::string> ld_library_paths_; | 
|  | 168 | std::vector<std::string> default_library_paths_; | 
|  | 169 | std::vector<std::string> permitted_paths_; | 
| Vic Yang | 2d020e4 | 2019-01-12 21:03:25 -0800 | [diff] [blame] | 170 | std::vector<std::string> whitelisted_libs_; | 
| Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 171 | // Loader looks into linked namespace if it was not able | 
|  | 172 | // to find a library in this namespace. Note that library | 
|  | 173 | // lookup in linked namespaces are limited by the list of | 
|  | 174 | // shared sonames. | 
|  | 175 | std::vector<android_namespace_link_t> linked_namespaces_; | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 176 | soinfo_list_t soinfo_list_; | 
|  | 177 |  | 
|  | 178 | DISALLOW_COPY_AND_ASSIGN(android_namespace_t); | 
|  | 179 | }; |