blob: a7fe0d55d2614b0ee0c5c5c93d387324f4f7aa07 [file] [log] [blame]
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001/*
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 Ivanov48ec2882016-08-04 11:50:36 -070034#include <string>
Dimitry Ivanovb943f302016-08-03 16:00:10 -070035#include <vector>
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080036#include <unordered_set>
37
38struct android_namespace_t;
39
40struct android_namespace_link_t {
41 public:
42 android_namespace_link_t(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +080043 const std::unordered_set<std::string>& shared_lib_sonames,
44 bool allow_all_shared_libs)
45 : linked_namespace_(linked_namespace), shared_lib_sonames_(shared_lib_sonames),
46 allow_all_shared_libs_(allow_all_shared_libs)
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080047 {}
48
49 android_namespace_t* linked_namespace() const {
50 return linked_namespace_;
51 }
52
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -070053 const std::unordered_set<std::string>& shared_lib_sonames() const {
54 return shared_lib_sonames_;
55 }
56
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080057 bool is_accessible(const char* soname) const {
Logan Chien9ee45912018-01-18 12:05:09 +080058 return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end();
59 }
60
61 bool allow_all_shared_libs() const {
62 return allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080063 }
64
65 private:
66 android_namespace_t* const linked_namespace_;
67 const std::unordered_set<std::string> shared_lib_sonames_;
Logan Chien9ee45912018-01-18 12:05:09 +080068 bool allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080069};
Dimitry Ivanovb943f302016-08-03 16:00:10 -070070
71struct android_namespace_t {
72 public:
Jiyong Park37b91af2017-05-05 22:07:05 +090073 android_namespace_t() : name_(nullptr), is_isolated_(false), is_greylist_enabled_(false) {}
Dimitry Ivanovb943f302016-08-03 16:00:10 -070074
75 const char* get_name() const { return name_; }
76 void set_name(const char* name) { name_ = name; }
77
78 bool is_isolated() const { return is_isolated_; }
79 void set_isolated(bool isolated) { is_isolated_ = isolated; }
80
Jiyong Park37b91af2017-05-05 22:07:05 +090081 bool is_greylist_enabled() const { return is_greylist_enabled_; }
82 void set_greylist_enabled(bool enabled) { is_greylist_enabled_ = enabled; }
83
Dimitry Ivanovb943f302016-08-03 16:00:10 -070084 const std::vector<std::string>& get_ld_library_paths() const {
85 return ld_library_paths_;
86 }
87 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
88 ld_library_paths_ = library_paths;
89 }
90
91 const std::vector<std::string>& get_default_library_paths() const {
92 return default_library_paths_;
93 }
94 void set_default_library_paths(std::vector<std::string>&& library_paths) {
95 default_library_paths_ = library_paths;
96 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080097 void set_default_library_paths(const std::vector<std::string>& library_paths) {
98 default_library_paths_ = library_paths;
99 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700100
101 const std::vector<std::string>& get_permitted_paths() const {
102 return permitted_paths_;
103 }
104 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
105 permitted_paths_ = permitted_paths;
106 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800107 void set_permitted_paths(const std::vector<std::string>& permitted_paths) {
108 permitted_paths_ = permitted_paths;
109 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700110
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800111 const std::vector<android_namespace_link_t>& linked_namespaces() const {
112 return linked_namespaces_;
113 }
114 void add_linked_namespace(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +0800115 const std::unordered_set<std::string>& shared_lib_sonames,
116 bool allow_all_shared_libs) {
117 linked_namespaces_.push_back(
118 android_namespace_link_t(linked_namespace, shared_lib_sonames, allow_all_shared_libs));
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800119 }
120
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700121 void add_soinfo(soinfo* si) {
122 soinfo_list_.push_back(si);
123 }
124
125 void add_soinfos(const soinfo_list_t& soinfos) {
126 for (auto si : soinfos) {
127 add_soinfo(si);
128 }
129 }
130
131 void remove_soinfo(soinfo* si) {
132 soinfo_list_.remove_if([&](soinfo* candidate) {
133 return si == candidate;
134 });
135 }
136
137 const soinfo_list_t& soinfo_list() const { return soinfo_list_; }
138
139 // For isolated namespaces - checks if the file is on the search path;
140 // always returns true for not isolated namespace.
141 bool is_accessible(const std::string& path);
142
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800143 // Returns true if si is accessible from this namespace. A soinfo
144 // is considered accessible when it belongs to this namespace
145 // or one of it's parent soinfos belongs to this namespace.
146 bool is_accessible(soinfo* si);
147
Jiyong Park02586a22017-05-20 01:01:24 +0900148 soinfo_list_t get_global_group();
149 soinfo_list_t get_shared_group();
150
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700151 private:
152 const char* name_;
153 bool is_isolated_;
Jiyong Park37b91af2017-05-05 22:07:05 +0900154 bool is_greylist_enabled_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700155 std::vector<std::string> ld_library_paths_;
156 std::vector<std::string> default_library_paths_;
157 std::vector<std::string> permitted_paths_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800158 // Loader looks into linked namespace if it was not able
159 // to find a library in this namespace. Note that library
160 // lookup in linked namespaces are limited by the list of
161 // shared sonames.
162 std::vector<android_namespace_link_t> linked_namespaces_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700163 soinfo_list_t soinfo_list_;
164
165 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
166};
167
168#endif /* __LINKER_NAMESPACES_H */