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