1 /*
2  * Copyright (C) 2017 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_CONFIG_H_
30 #define _LINKER_CONFIG_H_
31 
32 #include <android/api-level.h>
33 
34 #include <stdlib.h>
35 #include <limits.h>
36 #include "private/bionic_macros.h"
37 
38 #include <memory>
39 #include <string>
40 #include <vector>
41 #include <unordered_map>
42 
43 class NamespaceLinkConfig {
44  public:
45   NamespaceLinkConfig() = default;
NamespaceLinkConfig(const std::string & ns_name,const std::string & shared_libs)46   NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs)
47       : ns_name_(ns_name), shared_libs_(shared_libs)  {}
48 
ns_name()49   const std::string& ns_name() const {
50     return ns_name_;
51   }
52 
shared_libs()53   const std::string& shared_libs() const {
54     return shared_libs_;
55   }
56 
57  private:
58   std::string ns_name_;
59   std::string shared_libs_;
60 };
61 
62 class NamespaceConfig {
63  public:
NamespaceConfig(const std::string & name)64   explicit NamespaceConfig(const std::string& name)
65       : name_(name), isolated_(false), visible_(false)
66   {}
67 
name()68   const char* name() const {
69     return name_.c_str();
70   }
71 
isolated()72   bool isolated() const {
73     return isolated_;
74   }
75 
visible()76   bool visible() const {
77     return visible_;
78   }
79 
search_paths()80   const std::vector<std::string>& search_paths() const {
81     return search_paths_;
82   }
83 
permitted_paths()84   const std::vector<std::string>& permitted_paths() const {
85     return permitted_paths_;
86   }
87 
links()88   const std::vector<NamespaceLinkConfig>& links() const {
89     return namespace_links_;
90   }
91 
add_namespace_link(const std::string & ns_name,const std::string & shared_libs)92   void add_namespace_link(const std::string& ns_name, const std::string& shared_libs) {
93     namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs));
94   }
95 
set_isolated(bool isolated)96   void set_isolated(bool isolated) {
97     isolated_ = isolated;
98   }
99 
set_visible(bool visible)100   void set_visible(bool visible) {
101     visible_ = visible;
102   }
103 
set_search_paths(std::vector<std::string> && search_paths)104   void set_search_paths(std::vector<std::string>&& search_paths) {
105     search_paths_ = search_paths;
106   }
107 
set_permitted_paths(std::vector<std::string> && permitted_paths)108   void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
109     permitted_paths_ = permitted_paths;
110   }
111  private:
112   const std::string name_;
113   bool isolated_;
114   bool visible_;
115   std::vector<std::string> search_paths_;
116   std::vector<std::string> permitted_paths_;
117   std::vector<NamespaceLinkConfig> namespace_links_;
118 
119   DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig);
120 };
121 
122 class Config {
123  public:
Config()124   Config() : target_sdk_version_(__ANDROID_API__) {}
125 
namespace_configs()126   const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const {
127     return namespace_configs_;
128   }
129 
default_namespace_config()130   const NamespaceConfig* default_namespace_config() const {
131     auto it = namespace_configs_map_.find("default");
132     return it == namespace_configs_map_.end() ? nullptr : it->second;
133   }
134 
target_sdk_version()135   uint32_t target_sdk_version() const {
136     return target_sdk_version_;
137   }
138 
139   // note that this is one time event and therefore there is no need to
140   // read every section of the config. Every linker instance needs at
141   // most one configuration.
142   // Returns false in case of an error. If binary config was not found
143   // sets *config = nullptr.
144   static bool read_binary_config(const char* ld_config_file_path,
145                                  const char* binary_realpath,
146                                  bool is_asan,
147                                  const Config** config,
148                                  std::string* error_msg);
149  private:
150   void clear();
151 
set_target_sdk_version(uint32_t target_sdk_version)152   void set_target_sdk_version(uint32_t target_sdk_version) {
153     target_sdk_version_ = target_sdk_version;
154   }
155 
156   NamespaceConfig* create_namespace_config(const std::string& name);
157 
158   std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_;
159   std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_;
160   uint32_t target_sdk_version_;
161 
162   DISALLOW_COPY_AND_ASSIGN(Config);
163 };
164 
165 #endif /* _LINKER_CONFIG_H_ */
166