blob: 03520d79da1a21ad510cb1c2cc893c93975d28ab [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
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dimitry Ivanovb943f302016-08-03 16:00:10 -070030
31#include "linker_common_types.h"
32
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070033#include <string>
Dimitry Ivanovb943f302016-08-03 16:00:10 -070034#include <vector>
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080035#include <unordered_set>
36
37struct android_namespace_t;
38
39struct android_namespace_link_t {
40 public:
41 android_namespace_link_t(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +080042 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 Ivanov7d429d32017-02-01 15:28:52 -080046 {}
47
48 android_namespace_t* linked_namespace() const {
49 return linked_namespace_;
50 }
51
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -070052 const std::unordered_set<std::string>& shared_lib_sonames() const {
53 return shared_lib_sonames_;
54 }
55
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080056 bool is_accessible(const char* soname) const {
Logan Chien9ee45912018-01-18 12:05:09 +080057 return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end();
58 }
59
60 bool allow_all_shared_libs() const {
61 return allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080062 }
63
64 private:
65 android_namespace_t* const linked_namespace_;
66 const std::unordered_set<std::string> shared_lib_sonames_;
Logan Chien9ee45912018-01-18 12:05:09 +080067 bool allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080068};
Dimitry Ivanovb943f302016-08-03 16:00:10 -070069
70struct android_namespace_t {
71 public:
Jiyong Park37b91af2017-05-05 22:07:05 +090072 android_namespace_t() : name_(nullptr), is_isolated_(false), is_greylist_enabled_(false) {}
Dimitry Ivanovb943f302016-08-03 16:00:10 -070073
74 const char* get_name() const { return name_; }
75 void set_name(const char* name) { name_ = name; }
76
77 bool is_isolated() const { return is_isolated_; }
78 void set_isolated(bool isolated) { is_isolated_ = isolated; }
79
Jiyong Park37b91af2017-05-05 22:07:05 +090080 bool is_greylist_enabled() const { return is_greylist_enabled_; }
81 void set_greylist_enabled(bool enabled) { is_greylist_enabled_ = enabled; }
82
Dimitry Ivanovb943f302016-08-03 16:00:10 -070083 const std::vector<std::string>& get_ld_library_paths() const {
84 return ld_library_paths_;
85 }
86 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
87 ld_library_paths_ = library_paths;
88 }
89
90 const std::vector<std::string>& get_default_library_paths() const {
91 return default_library_paths_;
92 }
93 void set_default_library_paths(std::vector<std::string>&& library_paths) {
94 default_library_paths_ = library_paths;
95 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080096 void set_default_library_paths(const std::vector<std::string>& library_paths) {
97 default_library_paths_ = library_paths;
98 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -070099
100 const std::vector<std::string>& get_permitted_paths() const {
101 return permitted_paths_;
102 }
103 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
104 permitted_paths_ = permitted_paths;
105 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800106 void set_permitted_paths(const std::vector<std::string>& permitted_paths) {
107 permitted_paths_ = permitted_paths;
108 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700109
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800110 const std::vector<android_namespace_link_t>& linked_namespaces() const {
111 return linked_namespaces_;
112 }
113 void add_linked_namespace(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +0800114 const std::unordered_set<std::string>& shared_lib_sonames,
115 bool allow_all_shared_libs) {
116 linked_namespaces_.push_back(
117 android_namespace_link_t(linked_namespace, shared_lib_sonames, allow_all_shared_libs));
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800118 }
119
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700120 void add_soinfo(soinfo* si) {
121 soinfo_list_.push_back(si);
122 }
123
124 void add_soinfos(const soinfo_list_t& soinfos) {
125 for (auto si : soinfos) {
126 add_soinfo(si);
127 }
128 }
129
130 void remove_soinfo(soinfo* si) {
131 soinfo_list_.remove_if([&](soinfo* candidate) {
132 return si == candidate;
133 });
134 }
135
136 const soinfo_list_t& soinfo_list() const { return soinfo_list_; }
137
138 // For isolated namespaces - checks if the file is on the search path;
139 // always returns true for not isolated namespace.
140 bool is_accessible(const std::string& path);
141
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800142 // Returns true if si is accessible from this namespace. A soinfo
143 // is considered accessible when it belongs to this namespace
144 // or one of it's parent soinfos belongs to this namespace.
145 bool is_accessible(soinfo* si);
146
Jiyong Park02586a22017-05-20 01:01:24 +0900147 soinfo_list_t get_global_group();
148 soinfo_list_t get_shared_group();
149
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700150 private:
151 const char* name_;
152 bool is_isolated_;
Jiyong Park37b91af2017-05-05 22:07:05 +0900153 bool is_greylist_enabled_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700154 std::vector<std::string> ld_library_paths_;
155 std::vector<std::string> default_library_paths_;
156 std::vector<std::string> permitted_paths_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800157 // Loader looks into linked namespace if it was not able
158 // to find a library in this namespace. Note that library
159 // lookup in linked namespaces are limited by the list of
160 // shared sonames.
161 std::vector<android_namespace_link_t> linked_namespaces_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700162 soinfo_list_t soinfo_list_;
163
164 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
165};