blob: 77b662294dfffee5f8d209d41e3fd5d34af3ce84 [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
Ryan Prichard40494402020-03-24 22:37:50 -070037std::vector<std::string> fix_lib_paths(std::vector<std::string> paths);
38
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080039struct android_namespace_t;
40
41struct android_namespace_link_t {
42 public:
43 android_namespace_link_t(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +080044 const std::unordered_set<std::string>& shared_lib_sonames,
45 bool allow_all_shared_libs)
46 : linked_namespace_(linked_namespace), shared_lib_sonames_(shared_lib_sonames),
47 allow_all_shared_libs_(allow_all_shared_libs)
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080048 {}
49
50 android_namespace_t* linked_namespace() const {
51 return linked_namespace_;
52 }
53
Dimitry Ivanovf1cb6692017-05-01 17:45:38 -070054 const std::unordered_set<std::string>& shared_lib_sonames() const {
55 return shared_lib_sonames_;
56 }
57
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080058 bool is_accessible(const char* soname) const {
dimitry94f7a872018-04-27 12:19:07 +020059 if (soname == nullptr) {
60 return false;
61 }
Logan Chien9ee45912018-01-18 12:05:09 +080062 return allow_all_shared_libs_ || shared_lib_sonames_.find(soname) != shared_lib_sonames_.end();
63 }
64
65 bool allow_all_shared_libs() const {
66 return allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080067 }
68
69 private:
70 android_namespace_t* const linked_namespace_;
71 const std::unordered_set<std::string> shared_lib_sonames_;
Logan Chien9ee45912018-01-18 12:05:09 +080072 bool allow_all_shared_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -080073};
Dimitry Ivanovb943f302016-08-03 16:00:10 -070074
75struct android_namespace_t {
76 public:
Jiyong Park25bedfd2019-05-16 21:00:39 +090077 android_namespace_t() :
78 is_isolated_(false),
Ryan Prichardaff9a342020-08-03 15:29:12 -070079 is_exempt_list_enabled_(false),
Jiyong Park25bedfd2019-05-16 21:00:39 +090080 is_also_used_as_anonymous_(false) {}
Dimitry Ivanovb943f302016-08-03 16:00:10 -070081
Jiyong Parkb66a78b2019-05-20 11:04:18 +090082 const char* get_name() const { return name_.c_str(); }
Dimitry Ivanovb943f302016-08-03 16:00:10 -070083 void set_name(const char* name) { name_ = name; }
84
85 bool is_isolated() const { return is_isolated_; }
86 void set_isolated(bool isolated) { is_isolated_ = isolated; }
87
Ryan Prichardaff9a342020-08-03 15:29:12 -070088 bool is_exempt_list_enabled() const { return is_exempt_list_enabled_; }
89 void set_exempt_list_enabled(bool enabled) { is_exempt_list_enabled_ = enabled; }
Jiyong Park37b91af2017-05-05 22:07:05 +090090
Jiyong Park25bedfd2019-05-16 21:00:39 +090091 bool is_also_used_as_anonymous() const { return is_also_used_as_anonymous_; }
92 void set_also_used_as_anonymous(bool yes) { is_also_used_as_anonymous_ = yes; }
93
Dimitry Ivanovb943f302016-08-03 16:00:10 -070094 const std::vector<std::string>& get_ld_library_paths() const {
95 return ld_library_paths_;
96 }
97 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
Vic Yang976d4b42019-03-12 13:38:30 -070098 ld_library_paths_ = std::move(library_paths);
Dimitry Ivanovb943f302016-08-03 16:00:10 -070099 }
100
101 const std::vector<std::string>& get_default_library_paths() const {
102 return default_library_paths_;
103 }
104 void set_default_library_paths(std::vector<std::string>&& library_paths) {
Ryan Prichard40494402020-03-24 22:37:50 -0700105 default_library_paths_ = fix_lib_paths(std::move(library_paths));
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700106 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800107 void set_default_library_paths(const std::vector<std::string>& library_paths) {
Ryan Prichard40494402020-03-24 22:37:50 -0700108 default_library_paths_ = fix_lib_paths(library_paths);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800109 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700110
111 const std::vector<std::string>& get_permitted_paths() const {
112 return permitted_paths_;
113 }
114 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
Vic Yang976d4b42019-03-12 13:38:30 -0700115 permitted_paths_ = std::move(permitted_paths);
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700116 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800117 void set_permitted_paths(const std::vector<std::string>& permitted_paths) {
118 permitted_paths_ = permitted_paths;
119 }
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700120
Luke Huang30f2f052020-07-30 15:09:18 +0800121 const std::vector<std::string>& get_allowed_libs() const { return allowed_libs_; }
122 void set_allowed_libs(std::vector<std::string>&& allowed_libs) {
123 allowed_libs_ = std::move(allowed_libs);
Vic Yang2d020e42019-01-12 21:03:25 -0800124 }
Luke Huang30f2f052020-07-30 15:09:18 +0800125 void set_allowed_libs(const std::vector<std::string>& allowed_libs) {
126 allowed_libs_ = allowed_libs;
Vic Yang2d020e42019-01-12 21:03:25 -0800127 }
128
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800129 const std::vector<android_namespace_link_t>& linked_namespaces() const {
130 return linked_namespaces_;
131 }
132 void add_linked_namespace(android_namespace_t* linked_namespace,
Logan Chien9ee45912018-01-18 12:05:09 +0800133 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));
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800137 }
138
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700139 void add_soinfo(soinfo* si) {
140 soinfo_list_.push_back(si);
141 }
142
143 void add_soinfos(const soinfo_list_t& soinfos) {
144 for (auto si : soinfos) {
145 add_soinfo(si);
146 }
147 }
148
149 void remove_soinfo(soinfo* si) {
150 soinfo_list_.remove_if([&](soinfo* candidate) {
151 return si == candidate;
152 });
153 }
154
155 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
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800161 // 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
Jiyong Park02586a22017-05-20 01:01:24 +0900166 soinfo_list_t get_global_group();
167 soinfo_list_t get_shared_group();
168
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700169 private:
Jiyong Parkb66a78b2019-05-20 11:04:18 +0900170 std::string name_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700171 bool is_isolated_;
Ryan Prichardaff9a342020-08-03 15:29:12 -0700172 bool is_exempt_list_enabled_;
Jiyong Park25bedfd2019-05-16 21:00:39 +0900173 bool is_also_used_as_anonymous_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700174 std::vector<std::string> ld_library_paths_;
175 std::vector<std::string> default_library_paths_;
176 std::vector<std::string> permitted_paths_;
Luke Huang30f2f052020-07-30 15:09:18 +0800177 std::vector<std::string> allowed_libs_;
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800178 // 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_;
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700183 soinfo_list_t soinfo_list_;
184
185 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
186};