blob: 73ae2ef70c48dd84dc73762042c9916d608b441f [file] [log] [blame]
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08001/*
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#include "linker_config.h"
30
31#include "linker_globals.h"
32#include "linker_debug.h"
33#include "linker_utils.h"
34
35#include <android-base/file.h>
Justin Yun53ce7422017-11-27 16:28:07 +090036#include <android-base/properties.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070037#include <android-base/scopeguard.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080038#include <android-base/strings.h>
39
Christopher Ferris7a3681e2017-04-24 17:48:32 -070040#include <async_safe/log.h>
41
Inseob Kim216323b2018-06-18 15:30:18 +090042#include <limits.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080043#include <stdlib.h>
Jiyong Park42e81982019-01-25 18:18:01 +090044#include <unistd.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080045
46#include <string>
47#include <unordered_map>
48
49class ConfigParser {
50 public:
51 enum {
Jiyong Park8b029512017-11-29 18:30:53 +090052 kPropertyAssign,
53 kPropertyAppend,
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080054 kSection,
55 kEndOfFile,
56 kError,
57 };
58
59 explicit ConfigParser(std::string&& content)
Vic Yang976d4b42019-03-12 13:38:30 -070060 : content_(std::move(content)), p_(0), lineno_(0), was_end_of_file_(false) {}
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080061
62 /*
63 * Possible return values
Jiyong Park8b029512017-11-29 18:30:53 +090064 * kPropertyAssign: name is set to property name and value is set to property value
65 * kPropertyAppend: same as kPropertyAssign, but the value should be appended
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080066 * kSection: name is set to section name.
67 * kEndOfFile: reached end of file.
68 * kError: error_msg is set.
69 */
70 int next_token(std::string* name, std::string* value, std::string* error_msg) {
71 std::string line;
72 while(NextLine(&line)) {
73 size_t found = line.find('#');
74 line = android::base::Trim(line.substr(0, found));
75
76 if (line.empty()) {
77 continue;
78 }
79
Ryan Prichard92b3e1b2019-03-11 17:27:52 -070080 if (line[0] == '[' && line.back() == ']') {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080081 *name = line.substr(1, line.size() - 2);
82 return kSection;
83 }
84
Jiyong Park8b029512017-11-29 18:30:53 +090085 size_t found_assign = line.find('=');
86 size_t found_append = line.find("+=");
87 if (found_assign != std::string::npos && found_append == std::string::npos) {
88 *name = android::base::Trim(line.substr(0, found_assign));
89 *value = android::base::Trim(line.substr(found_assign + 1));
90 return kPropertyAssign;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080091 }
92
Jiyong Park8b029512017-11-29 18:30:53 +090093 if (found_append != std::string::npos) {
94 *name = android::base::Trim(line.substr(0, found_append));
95 *value = android::base::Trim(line.substr(found_append + 2));
96 return kPropertyAppend;
97 }
98
99 *error_msg = std::string("invalid format: ") +
100 line +
101 ", expected \"name = property\", \"name += property\", or \"[section]\"";
102 return kError;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800103 }
104
105 // to avoid infinite cycles when programmer makes a mistake
106 CHECK(!was_end_of_file_);
107 was_end_of_file_ = true;
108 return kEndOfFile;
109 }
110
111 size_t lineno() const {
112 return lineno_;
113 }
114
115 private:
116 bool NextLine(std::string* line) {
117 if (p_ == std::string::npos) {
118 return false;
119 }
120
121 size_t found = content_.find('\n', p_);
122 if (found != std::string::npos) {
123 *line = content_.substr(p_, found - p_);
124 p_ = found + 1;
125 } else {
126 *line = content_.substr(p_);
127 p_ = std::string::npos;
128 }
129
130 lineno_++;
131 return true;
132 }
133
134 std::string content_;
135 size_t p_;
136 size_t lineno_;
137 bool was_end_of_file_;
138
139 DISALLOW_IMPLICIT_CONSTRUCTORS(ConfigParser);
140};
141
142class PropertyValue {
143 public:
144 PropertyValue() = default;
145
146 PropertyValue(std::string&& value, size_t lineno)
Vic Yang976d4b42019-03-12 13:38:30 -0700147 : value_(std::move(value)), lineno_(lineno) {}
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800148
149 const std::string& value() const {
150 return value_;
151 }
152
Jiyong Park8b029512017-11-29 18:30:53 +0900153 void append_value(std::string&& value) {
154 value_ = value_ + value;
155 // lineno isn't updated as we might have cases like this:
156 // property.x = blah
157 // property.y = blah
158 // property.x += blah
159 }
160
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800161 size_t lineno() const {
162 return lineno_;
163 }
164
165 private:
166 std::string value_;
167 size_t lineno_;
168};
169
170static std::string create_error_msg(const char* file,
171 size_t lineno,
172 const std::string& msg) {
173 char buf[1024];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700174 async_safe_format_buffer(buf, sizeof(buf), "%s:%zu: error: %s", file, lineno, msg.c_str());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800175
176 return std::string(buf);
177}
178
179static bool parse_config_file(const char* ld_config_file_path,
180 const char* binary_realpath,
181 std::unordered_map<std::string, PropertyValue>* properties,
182 std::string* error_msg) {
183 std::string content;
184 if (!android::base::ReadFileToString(ld_config_file_path, &content)) {
185 if (errno != ENOENT) {
186 *error_msg = std::string("error reading file \"") +
187 ld_config_file_path + "\": " + strerror(errno);
188 }
189 return false;
190 }
191
192 ConfigParser cp(std::move(content));
193
194 std::string section_name;
195
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800196 while (true) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800197 std::string name;
198 std::string value;
199 std::string error;
200
201 int result = cp.next_token(&name, &value, &error);
202 if (result == ConfigParser::kError) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800203 DL_WARN("%s:%zd: warning: couldn't parse %s (ignoring this line)",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800204 ld_config_file_path,
205 cp.lineno(),
206 error.c_str());
207 continue;
208 }
209
210 if (result == ConfigParser::kSection || result == ConfigParser::kEndOfFile) {
211 return false;
212 }
213
Jiyong Park8b029512017-11-29 18:30:53 +0900214 if (result == ConfigParser::kPropertyAssign) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800215 if (!android::base::StartsWith(name, "dir.")) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800216 DL_WARN("%s:%zd: warning: unexpected property name \"%s\", "
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800217 "expected format dir.<section_name> (ignoring this line)",
218 ld_config_file_path,
219 cp.lineno(),
220 name.c_str());
221 continue;
222 }
223
224 // remove trailing '/'
Inseob Kim216323b2018-06-18 15:30:18 +0900225 while (!value.empty() && value.back() == '/') {
226 value.pop_back();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800227 }
228
229 if (value.empty()) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800230 DL_WARN("%s:%zd: warning: property value is empty (ignoring this line)",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800231 ld_config_file_path,
232 cp.lineno());
233 continue;
234 }
235
Inseob Kim216323b2018-06-18 15:30:18 +0900236 // If the path can be resolved, resolve it
237 char buf[PATH_MAX];
238 std::string resolved_path;
Jiyong Park42e81982019-01-25 18:18:01 +0900239 if (access(value.c_str(), R_OK) != 0) {
240 if (errno == ENOENT) {
241 // no need to test for non-existing path. skip.
242 continue;
243 }
244 // If not accessible, don't call realpath as it will just cause
245 // SELinux denial spam. Use the path unresolved.
246 resolved_path = value;
247 } else if (realpath(value.c_str(), buf)) {
Inseob Kim216323b2018-06-18 15:30:18 +0900248 resolved_path = buf;
Jiyong Park42e81982019-01-25 18:18:01 +0900249 } else {
Ryan Prichard6b55cc32019-01-04 14:58:26 -0800250 // realpath is expected to fail with EPERM in some situations, so log
251 // the failure with INFO rather than DL_WARN. e.g. A binary in
252 // /data/local/tmp may attempt to stat /postinstall. See
253 // http://b/120996057.
Elliott Hughesf5e21d92024-07-26 11:48:19 +0000254 INFO("%s:%zd: warning: path \"%s\" couldn't be resolved: %m",
Ryan Prichard6b55cc32019-01-04 14:58:26 -0800255 ld_config_file_path,
256 cp.lineno(),
Elliott Hughesf5e21d92024-07-26 11:48:19 +0000257 value.c_str());
Inseob Kim216323b2018-06-18 15:30:18 +0900258 resolved_path = value;
259 }
260
261 if (file_is_under_dir(binary_realpath, resolved_path)) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800262 section_name = name.substr(4);
263 break;
264 }
265 }
266 }
267
Martin Stjernholm95252ee2019-02-22 22:48:59 +0000268 INFO("[ Using config section \"%s\" ]", section_name.c_str());
269
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800270 // skip everything until we meet a correct section
271 while (true) {
272 std::string name;
273 std::string value;
274 std::string error;
275
276 int result = cp.next_token(&name, &value, &error);
277
278 if (result == ConfigParser::kSection && name == section_name) {
279 break;
280 }
281
282 if (result == ConfigParser::kEndOfFile) {
283 *error_msg = create_error_msg(ld_config_file_path,
284 cp.lineno(),
285 std::string("section \"") + section_name + "\" not found");
286 return false;
287 }
288 }
289
290 // found the section - parse it
291 while (true) {
292 std::string name;
293 std::string value;
294 std::string error;
295
296 int result = cp.next_token(&name, &value, &error);
297
298 if (result == ConfigParser::kEndOfFile || result == ConfigParser::kSection) {
299 break;
300 }
301
Jiyong Park8b029512017-11-29 18:30:53 +0900302 if (result == ConfigParser::kPropertyAssign) {
Elliott Hughes192f3cf2024-06-25 11:21:41 +0000303 if (properties->contains(name)) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800304 DL_WARN("%s:%zd: warning: redefining property \"%s\" (overriding previous value)",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800305 ld_config_file_path,
306 cp.lineno(),
307 name.c_str());
308 }
309
310 (*properties)[name] = PropertyValue(std::move(value), cp.lineno());
Jiyong Park8b029512017-11-29 18:30:53 +0900311 } else if (result == ConfigParser::kPropertyAppend) {
Elliott Hughes192f3cf2024-06-25 11:21:41 +0000312 if (!properties->contains(name)) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800313 DL_WARN("%s:%zd: warning: appending to undefined property \"%s\" (treating as assignment)",
Jiyong Park8b029512017-11-29 18:30:53 +0900314 ld_config_file_path,
315 cp.lineno(),
316 name.c_str());
317 (*properties)[name] = PropertyValue(std::move(value), cp.lineno());
318 } else {
319 if (android::base::EndsWith(name, ".links") ||
320 android::base::EndsWith(name, ".namespaces")) {
321 value = "," + value;
322 (*properties)[name].append_value(std::move(value));
323 } else if (android::base::EndsWith(name, ".paths") ||
Jooyung Han61a9a402020-06-02 15:38:49 +0900324 android::base::EndsWith(name, ".shared_libs") ||
Luke Huang30f2f052020-07-30 15:09:18 +0800325 android::base::EndsWith(name, ".whitelisted") ||
326 android::base::EndsWith(name, ".allowed_libs")) {
Jiyong Park8b029512017-11-29 18:30:53 +0900327 value = ":" + value;
328 (*properties)[name].append_value(std::move(value));
329 } else {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800330 DL_WARN("%s:%zd: warning: += isn't allowed for property \"%s\" (ignoring)",
Jiyong Park8b029512017-11-29 18:30:53 +0900331 ld_config_file_path,
332 cp.lineno(),
333 name.c_str());
334 }
335 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800336 }
337
338 if (result == ConfigParser::kError) {
Elliott Hughes9076b0c2018-02-28 11:29:45 -0800339 DL_WARN("%s:%zd: warning: couldn't parse %s (ignoring this line)",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800340 ld_config_file_path,
341 cp.lineno(),
342 error.c_str());
343 continue;
344 }
345 }
346
347 return true;
348}
349
350static Config g_config;
351
352static constexpr const char* kDefaultConfigName = "default";
353static constexpr const char* kPropertyAdditionalNamespaces = "additional.namespaces";
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800354
355class Properties {
356 public:
357 explicit Properties(std::unordered_map<std::string, PropertyValue>&& properties)
Vic Yang976d4b42019-03-12 13:38:30 -0700358 : properties_(std::move(properties)), target_sdk_version_(__ANDROID_API__) {}
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800359
360 std::vector<std::string> get_strings(const std::string& name, size_t* lineno = nullptr) const {
361 auto it = find_property(name, lineno);
362 if (it == properties_.end()) {
363 // return empty vector
364 return std::vector<std::string>();
365 }
366
367 std::vector<std::string> strings = android::base::Split(it->second.value(), ",");
368
369 for (size_t i = 0; i < strings.size(); ++i) {
370 strings[i] = android::base::Trim(strings[i]);
371 }
372
373 return strings;
374 }
375
376 bool get_bool(const std::string& name, size_t* lineno = nullptr) const {
377 auto it = find_property(name, lineno);
378 if (it == properties_.end()) {
379 return false;
380 }
381
382 return it->second.value() == "true";
383 }
384
385 std::string get_string(const std::string& name, size_t* lineno = nullptr) const {
386 auto it = find_property(name, lineno);
387 return (it == properties_.end()) ? "" : it->second.value();
388 }
389
Jiyong Park0f33f232017-09-21 10:27:36 +0900390 std::vector<std::string> get_paths(const std::string& name, bool resolve, size_t* lineno = nullptr) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800391 std::string paths_str = get_string(name, lineno);
392
393 std::vector<std::string> paths;
394 split_path(paths_str.c_str(), ":", &paths);
395
396 std::vector<std::pair<std::string, std::string>> params;
Ryan Prichard4d4087d2019-12-02 16:55:48 -0800397 params.push_back({ "LIB", kLibPath });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800398 if (target_sdk_version_ != 0) {
399 char buf[16];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700400 async_safe_format_buffer(buf, sizeof(buf), "%d", target_sdk_version_);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800401 params.push_back({ "SDK_VER", buf });
402 }
403
Jooyung Han09283992019-10-29 05:29:56 +0900404 static std::string vndk_ver = Config::get_vndk_version_string('-');
405 params.push_back({ "VNDK_VER", vndk_ver });
406 static std::string vndk_apex_ver = Config::get_vndk_version_string('v');
407 params.push_back({ "VNDK_APEX_VER", vndk_apex_ver });
Jooyung Han5d1d9072019-08-26 23:08:33 +0000408
Vic Yang976d4b42019-03-12 13:38:30 -0700409 for (auto& path : paths) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800410 format_string(&path, params);
411 }
412
Jiyong Park0f33f232017-09-21 10:27:36 +0900413 if (resolve) {
414 std::vector<std::string> resolved_paths;
Jiyong Park341b61e2019-05-13 16:52:01 +0900415 for (const auto& path : paths) {
416 if (path.empty()) {
417 continue;
418 }
419 // this is single threaded. no need to lock
420 auto cached = resolved_paths_.find(path);
421 if (cached == resolved_paths_.end()) {
422 resolved_paths_[path] = resolve_path(path);
423 cached = resolved_paths_.find(path);
424 }
425 CHECK(cached != resolved_paths_.end());
426 if (cached->second.empty()) {
427 continue;
428 }
429 resolved_paths.push_back(cached->second);
430 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800431
Jiyong Park0f33f232017-09-21 10:27:36 +0900432 return resolved_paths;
433 } else {
434 return paths;
435 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800436 }
437
438 void set_target_sdk_version(int target_sdk_version) {
439 target_sdk_version_ = target_sdk_version;
440 }
441
442 private:
443 std::unordered_map<std::string, PropertyValue>::const_iterator
444 find_property(const std::string& name, size_t* lineno) const {
445 auto it = properties_.find(name);
446 if (it != properties_.end() && lineno != nullptr) {
447 *lineno = it->second.lineno();
448 }
449
450 return it;
451 }
452 std::unordered_map<std::string, PropertyValue> properties_;
Jiyong Park341b61e2019-05-13 16:52:01 +0900453 std::unordered_map<std::string, std::string> resolved_paths_;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800454 int target_sdk_version_;
455
456 DISALLOW_IMPLICIT_CONSTRUCTORS(Properties);
457};
458
459bool Config::read_binary_config(const char* ld_config_file_path,
460 const char* binary_realpath,
461 bool is_asan,
Florian Mayerc10d0642023-03-22 16:12:49 -0700462 bool is_hwasan,
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800463 const Config** config,
464 std::string* error_msg) {
465 g_config.clear();
466
467 std::unordered_map<std::string, PropertyValue> property_map;
468 if (!parse_config_file(ld_config_file_path, binary_realpath, &property_map, error_msg)) {
469 return false;
470 }
471
472 Properties properties(std::move(property_map));
473
Tom Cherryb8ab6182017-04-05 16:20:29 -0700474 auto failure_guard = android::base::make_scope_guard([] { g_config.clear(); });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800475
476 std::unordered_map<std::string, NamespaceConfig*> namespace_configs;
477
478 namespace_configs[kDefaultConfigName] = g_config.create_namespace_config(kDefaultConfigName);
479
480 std::vector<std::string> additional_namespaces = properties.get_strings(kPropertyAdditionalNamespaces);
481 for (const auto& name : additional_namespaces) {
482 namespace_configs[name] = g_config.create_namespace_config(name);
483 }
484
485 bool versioning_enabled = properties.get_bool("enable.target.sdk.version");
486 int target_sdk_version = __ANDROID_API__;
487 if (versioning_enabled) {
488 std::string version_file = dirname(binary_realpath) + "/.version";
489 std::string content;
490 if (!android::base::ReadFileToString(version_file, &content)) {
491 if (errno != ENOENT) {
492 *error_msg = std::string("error reading version file \"") +
493 version_file + "\": " + strerror(errno);
494 return false;
495 }
496 } else {
497 content = android::base::Trim(content);
498 errno = 0;
499 char* end = nullptr;
500 const char* content_str = content.c_str();
501 int result = strtol(content_str, &end, 10);
502 if (errno == 0 && *end == '\0' && result > 0) {
503 target_sdk_version = result;
504 properties.set_target_sdk_version(target_sdk_version);
505 } else {
506 *error_msg = std::string("invalid version \"") + version_file + "\": \"" + content +"\"";
507 return false;
508 }
509 }
510 }
511
512 g_config.set_target_sdk_version(target_sdk_version);
513
Chih-Hung Hsieh0218e922018-12-11 10:22:11 -0800514 for (const auto& ns_config_it : namespace_configs) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800515 auto& name = ns_config_it.first;
516 NamespaceConfig* ns_config = ns_config_it.second;
517
518 std::string property_name_prefix = std::string("namespace.") + name;
519
520 size_t lineno = 0;
521 std::vector<std::string> linked_namespaces =
522 properties.get_strings(property_name_prefix + ".links", &lineno);
523
524 for (const auto& linked_ns_name : linked_namespaces) {
Elliott Hughes192f3cf2024-06-25 11:21:41 +0000525 if (!namespace_configs.contains(linked_ns_name)) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800526 *error_msg = create_error_msg(ld_config_file_path,
527 lineno,
528 std::string("undefined namespace: ") + linked_ns_name);
529 return false;
530 }
531
Logan Chien9ee45912018-01-18 12:05:09 +0800532 bool allow_all_shared_libs = properties.get_bool(property_name_prefix + ".link." +
533 linked_ns_name + ".allow_all_shared_libs");
534
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800535 std::string shared_libs = properties.get_string(property_name_prefix +
536 ".link." +
537 linked_ns_name +
538 ".shared_libs", &lineno);
539
Logan Chien9ee45912018-01-18 12:05:09 +0800540 if (!allow_all_shared_libs && shared_libs.empty()) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800541 *error_msg = create_error_msg(ld_config_file_path,
542 lineno,
543 std::string("list of shared_libs for ") +
544 name +
545 "->" +
546 linked_ns_name +
547 " link is not specified or is empty.");
548 return false;
549 }
550
Logan Chien9ee45912018-01-18 12:05:09 +0800551 if (allow_all_shared_libs && !shared_libs.empty()) {
552 *error_msg = create_error_msg(ld_config_file_path, lineno,
553 std::string("both shared_libs and allow_all_shared_libs "
554 "are set for ") +
555 name + "->" + linked_ns_name + " link.");
556 return false;
557 }
558
559 ns_config->add_namespace_link(linked_ns_name, shared_libs, allow_all_shared_libs);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800560 }
561
562 ns_config->set_isolated(properties.get_bool(property_name_prefix + ".isolated"));
Jiyong Park01de74e2017-04-03 23:10:37 +0900563 ns_config->set_visible(properties.get_bool(property_name_prefix + ".visible"));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800564
Luke Huang30f2f052020-07-30 15:09:18 +0800565 std::string allowed_libs =
Vic Yang2d020e42019-01-12 21:03:25 -0800566 properties.get_string(property_name_prefix + ".whitelisted", &lineno);
Luke Huang30f2f052020-07-30 15:09:18 +0800567 const std::string libs = properties.get_string(property_name_prefix + ".allowed_libs", &lineno);
568 if (!allowed_libs.empty() && !libs.empty()) {
569 allowed_libs += ":";
570 }
571 allowed_libs += libs;
572 if (!allowed_libs.empty()) {
573 ns_config->set_allowed_libs(android::base::Split(allowed_libs, ":"));
Vic Yang2d020e42019-01-12 21:03:25 -0800574 }
575
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800576 // these are affected by is_asan flag
577 if (is_asan) {
578 property_name_prefix += ".asan";
Florian Mayerc10d0642023-03-22 16:12:49 -0700579 } else if (is_hwasan) {
580 property_name_prefix += ".hwasan";
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800581 }
582
Jiyong Park0f33f232017-09-21 10:27:36 +0900583 // search paths are resolved (canonicalized). This is required mainly for
584 // the case when /vendor is a symlink to /system/vendor, which is true for
585 // non Treble-ized legacy devices.
586 ns_config->set_search_paths(properties.get_paths(property_name_prefix + ".search.paths", true));
587
588 // However, for permitted paths, we are not required to resolve the paths
589 // since they are only set for isolated namespaces, which implies the device
590 // is Treble-ized (= /vendor is not a symlink to /system/vendor).
591 // In fact, the resolving is causing an unexpected side effect of selinux
592 // denials on some executables which are not allowed to access some of the
593 // permitted paths.
594 ns_config->set_permitted_paths(properties.get_paths(property_name_prefix + ".permitted.paths", false));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800595 }
596
Tom Cherryb8ab6182017-04-05 16:20:29 -0700597 failure_guard.Disable();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800598 *config = &g_config;
599 return true;
600}
601
Jooyung Hana365ac12019-10-16 22:37:10 +0000602std::string Config::get_vndk_version_string(const char delimiter) {
Justin Yun53ce7422017-11-27 16:28:07 +0900603 std::string version = android::base::GetProperty("ro.vndk.version", "");
604 if (version != "" && version != "current") {
Jooyung Hana365ac12019-10-16 22:37:10 +0000605 //add the delimiter char in front of the string and return it.
606 return version.insert(0, 1, delimiter);
Justin Yun53ce7422017-11-27 16:28:07 +0900607 }
608 return "";
609}
610
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800611NamespaceConfig* Config::create_namespace_config(const std::string& name) {
612 namespace_configs_.push_back(std::unique_ptr<NamespaceConfig>(new NamespaceConfig(name)));
613 NamespaceConfig* ns_config_ptr = namespace_configs_.back().get();
614 namespace_configs_map_[name] = ns_config_ptr;
615 return ns_config_ptr;
616}
617
618void Config::clear() {
619 namespace_configs_.clear();
620 namespace_configs_map_.clear();
621}