1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "linkerconfig/namespace.h"
18
19 #include <android-base/strings.h>
20
21 #include "linkerconfig/apex.h"
22 #include "linkerconfig/log.h"
23
24 using android::base::Result;
25
26 namespace {
27 constexpr const char* kDataAsanPath = "/data/asan";
28
VerifyIfApexNamespaceContainsAllSharedLink(const android::linkerconfig::modules::Namespace & ns)29 Result<void> VerifyIfApexNamespaceContainsAllSharedLink(
30 const android::linkerconfig::modules::Namespace& ns) {
31 auto apex_name = ns.GetApexSource();
32 // If namespace is not from APEX there is no need to check this.
33 if (apex_name == "") {
34 return {};
35 }
36
37 const auto& links = ns.Links();
38 for (const auto& link : links) {
39 if (link.IsAllSharedLibsAllowed()) {
40 return Errorf(
41 "APEX namespace {} is not allowed to have link with all shared libs "
42 "allowed.",
43 ns.GetName());
44 }
45 }
46 return {};
47 }
48
49 } // namespace
50
51 namespace android {
52 namespace linkerconfig {
53 namespace modules {
54
InitializeWithApex(Namespace & ns,const ApexInfo & apex_info)55 void InitializeWithApex(Namespace& ns, const ApexInfo& apex_info) {
56 ns.AddSearchPath(apex_info.path + "/${LIB}");
57 ns.AddPermittedPath(apex_info.path + "/${LIB}");
58 ns.AddPermittedPath("/system/${LIB}");
59 for (const auto& permitted_path : apex_info.permitted_paths) {
60 ns.AddPermittedPath(permitted_path);
61 }
62 if (apex_info.has_shared_lib) {
63 ns.AddPermittedPath("/apex");
64 }
65 ns.AddProvides(apex_info.provide_libs);
66 ns.AddRequires(apex_info.require_libs);
67 ns.SetApexSource(apex_info.name);
68 }
69
GetLink(const std::string & target_namespace)70 Link& Namespace::GetLink(const std::string& target_namespace) {
71 for (auto& link : links_) {
72 if (link.To() == target_namespace) {
73 return link;
74 }
75 }
76 return links_.emplace_back(name_, target_namespace);
77 }
78
WriteConfig(ConfigWriter & writer)79 void Namespace::WriteConfig(ConfigWriter& writer) {
80 auto verify_result = VerifyContents();
81 if (!verify_result.ok()) {
82 LOG(ERROR) << "Namespace " << name_
83 << " is not valid : " << verify_result.error();
84 return;
85 }
86
87 const auto prefix = "namespace." + name_ + ".";
88
89 writer.WriteLine(prefix + "isolated = " + (is_isolated_ ? "true" : "false"));
90
91 if (is_visible_) {
92 writer.WriteLine(prefix + "visible = true");
93 }
94
95 writer.WriteVars(prefix + "search.paths", search_paths_);
96 writer.WriteVars(prefix + "permitted.paths", permitted_paths_);
97 writer.WriteVars(prefix + "asan.search.paths", asan_search_paths_);
98 writer.WriteVars(prefix + "asan.permitted.paths", asan_permitted_paths_);
99 writer.WriteVars(prefix + "allowed_libs", allowed_libs_);
100
101 std::vector<std::string> link_list;
102 link_list.reserve(links_.size());
103 for (const auto& link : links_) {
104 if (link.Empty()) continue;
105 link_list.push_back(link.To());
106 }
107 if (!link_list.empty()) {
108 writer.WriteLine(prefix + "links = " + android::base::Join(link_list, ","));
109 for (const auto& link : links_) {
110 if (link.Empty()) continue;
111 link.WriteConfig(writer);
112 }
113 }
114 }
115
AddSearchPath(const std::string & path)116 void Namespace::AddSearchPath(const std::string& path) {
117 search_paths_.push_back(path);
118
119 if (RequiresAsanPath(path)) {
120 asan_search_paths_.push_back(CreateAsanPath(path));
121 }
122 asan_search_paths_.push_back(path);
123 }
124
AddPermittedPath(const std::string & path)125 void Namespace::AddPermittedPath(const std::string& path) {
126 permitted_paths_.push_back(path);
127
128 if (RequiresAsanPath(path)) {
129 asan_permitted_paths_.push_back(CreateAsanPath(path));
130 }
131 asan_permitted_paths_.push_back(path);
132 }
133
AddAllowedLib(const std::string & path)134 void Namespace::AddAllowedLib(const std::string& path) {
135 allowed_libs_.push_back(path);
136 }
137
GetName() const138 std::string Namespace::GetName() const {
139 return name_;
140 }
141
RequiresAsanPath(const std::string & path)142 bool Namespace::RequiresAsanPath(const std::string& path) {
143 return !android::base::StartsWith(path, "/apex");
144 }
145
CreateAsanPath(const std::string & path)146 const std::string Namespace::CreateAsanPath(const std::string& path) {
147 return kDataAsanPath + path;
148 }
149
VerifyContents()150 Result<void> Namespace::VerifyContents() {
151 auto apex_with_all_shared_link =
152 VerifyIfApexNamespaceContainsAllSharedLink(*this);
153 if (!apex_with_all_shared_link.ok()) {
154 return apex_with_all_shared_link.error();
155 }
156
157 return {};
158 }
159
160 } // namespace modules
161 } // namespace linkerconfig
162 } // namespace android
163